需要有关静态/非静态成员的帮助

发布于 2024-10-06 05:13:21 字数 779 浏览 0 评论 0原文

我有一个如下所示的静态成员函数:

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
member whackamole_window::m' in
static member function
mole111j.cpp:177: invalid use of
member
whackamole_window::mb' in
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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

物价感观 2024-10-13 05:13:21

我对 FLTK 不太了解,但看起来您的 showMole() 方法传入了一个 void * 值,您可以在启动计时器时指定该值。

如果您在启动计时器时提供指向窗口的指针,则可以将 void * 值强制转换回 whackamole_window * 并使用生成的指针来访问非静态成员。

I don't know much about FLTK, but it looks like your showMole() method gets a void * 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 to whackamole_window * and use the resulting pointer to access non-static members.

晨与橙与城 2024-10-13 05:13:21

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文