glutTimerFunc 问题
我正在使用计时器函数来制作动画,但是当我将其放置在 Renderer 类中时遇到问题。
void Renderer::animate(int value)
{
glutTimerFunc(TIMERMSECS, animate, 0);
}
错误: \renderer.cpp(242) : 错误 C3867: 'Renderer::animate': 函数调用缺少参数列表;使用“&Renderer::animate”创建指向成员的指针
I am using a timer function to animate, but I am having issues when I place it in the Renderer class.
void Renderer::animate(int value)
{
glutTimerFunc(TIMERMSECS, animate, 0);
}
Error:
\renderer.cpp(242) : error C3867: 'Renderer::animate': function call missing argument list; use '&Renderer::animate' to create a pointer to member
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题是
Renderer::animate
是一个类成员函数,因此有一个隐藏的this
参数。glutTimerFunc
不知道要传递this
的值,因此,如果您以某种方式试图通过令人讨厌的强制转换来绕过编译器,它会在运行时崩溃。解决方案是将 Renderer::animate 更改为静态方法或普通的全局函数。然后,您需要将指向
Renderer
实例的指针存储在全局变量中,例如:If
sizeof(void*) <= sizeof(int)
(在 32- 上为 true)位系统,但在 64 位系统上并非如此),那么您还可以通过value
参数传递实例;但是,由于这是不可移植的,并且您不需要同时在多个不同的实例上调用 glutTimerFunc,因此您不必担心为此使用全局变量。The issue is that
Renderer::animate
is a class member function and so has a hiddenthis
parameter.glutTimerFunc
doesn't know what value ofthis
to pass, so if you somehow tried to sneak past the compiler with a nasty cast, it would crash at runtime.The solution is to change
Renderer::animate
into a static method or an ordinary global function. You then need to store the pointer to yourRenderer
instance in a global variable, e.g.:If
sizeof(void*) <= sizeof(int)
(true on 32-bit systems but not true on 64-bit systems), then you could also pass the instance via thevalue
parameter; however, since this is not portable and you won't ever need to callglutTimerFunc
on multiple different instances simultaneously, you shouldn't have to worry about using a global variable for this.glutTimerFunc()
需要一个指向void (*func)(int value)
类型的函数,而不是void (Renderer::*func)(int value)
类型的成员函数。将 Render::animate 设为静态或使用全局函数。
glutTimerFunc()
expects a pointer to a function of typevoid (*func)(int value)
, not a member function of typevoid (Renderer::*func)(int value)
.Make
Render::animate
static or use a global function.