需要有关静态/非静态成员的帮助
我有一个如下所示的静态成员函数:
void whackamole_window::showmole(void *){
if (mtime == 0) {
Fl::remove_timeout(whackamole_window::showmole);
}
else {
//...
m(Point(randx*50+25,randy*50+25),randval),
mb(Point(randx*50,randy*50),50,50,"1",cb_addscore)
Fl::check();
Fl::redraw();
mtime -= 3;
Fl::repeat_timeout(3, whackamole_window::showmole);
return;
}
}
成员函数在类中定义如下
static void showmole(void*);
由于静态成员无法访问类的任何其他成员(其他静态成员除外),因此出现以下错误:
mole111j.cpp:176: 无效使用 成员
whackamole_window::m' 中 静态成员函数 mole111j.cpp:177: 无效使用 成员
whackamole_window::mb' 中 静态成员函数
如果我尝试使函数成为非静态的,计时器似乎不起作用。那么我该如何解决这个问题并使用计时器访问 m 和 mb 成员呢?
I have a static member function that looks like this:
void whackamole_window::showmole(void *){
if (mtime == 0) {
Fl::remove_timeout(whackamole_window::showmole);
}
else {
//...
m(Point(randx*50+25,randy*50+25),randval),
mb(Point(randx*50,randy*50),50,50,"1",cb_addscore)
Fl::check();
Fl::redraw();
mtime -= 3;
Fl::repeat_timeout(3, whackamole_window::showmole);
return;
}
}
The member function is defined as the following in the class
static void showmole(void*);
Since static members can't access any other members of the class (except for other static members), I'm getting the following error:
mole111j.cpp:176: invalid use of
memberwhackamole_window::m' in
whackamole_window::mb' in
static member function
mole111j.cpp:177: invalid use of
member
static member function
The timer doesn't seem to work if I try to make the function non-static. So how can I work around this and access the m and mb members using the timer?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我对 FLTK 不太了解,但看起来您的
showMole()
方法传入了一个void *
值,您可以在启动计时器时指定该值。如果您在启动计时器时提供指向窗口的指针,则可以将
void *
值强制转换回whackamole_window *
并使用生成的指针来访问非静态成员。I don't know much about FLTK, but it looks like your
showMole()
method gets avoid *
value passed in, which you can presumably specify when you arm the timer.If you provide a pointer to the window when you arm the timer, you can cast the
void *
value back towhackamole_window *
and use the resulting pointer to access non-static members.m 和 mb 都是静态变量吗?如果不是,你就不能像你一样在静态函数中使用它们。
使它们静态或传递它们。
Are m and mb both static variables? If not you can't use them in a static function like you are.
Make them static or pass them in.