尝试在 c++ 中执行某些 OpenGl 时出现 C2664 错误
这是我的代码的摘要。我正在尝试使用 glutSpecialFunc 告诉 glut 使用我的 KeyPress 函数
class Car : public WorldObject
{
public:
void KeyPress(int key, int x, int y)
{
}
Car()
{
glutSpecialFunc(&Car::KeyPress); // C2664 error
}
}
我收到的错误消息是:
Error 1 error C2664: 'glutSpecialFunc' : cannot convert parameter 1 from 'void (__thiscall Car::* )(int,int,int)' to 'void (__cdecl *)(int,int,int)' c:\users\thorgeir\desktop\programmingproject1\quickness\quickness\car.cpp 18 Quickness
Here is an abstract of my code. I'm trying to use glutSpecialFunc to tell glut to use my KeyPress function
class Car : public WorldObject
{
public:
void KeyPress(int key, int x, int y)
{
}
Car()
{
glutSpecialFunc(&Car::KeyPress); // C2664 error
}
}
The error message I get is:
Error 1 error C2664: 'glutSpecialFunc' : cannot convert parameter 1 from 'void (__thiscall Car::* )(int,int,int)' to 'void (__cdecl *)(int,int,int)' c:\users\thorgeir\desktop\programmingproject1\quickness\quickness\car.cpp 18 Quickness
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你的函数是一个类的成员。当你做类似
Car c; 的事情时c.drive()
,该drive()
函数需要一辆汽车才能使用。这就是this
指针。因此,如果 glut 没有汽车可以使用,则它无法调用该函数,它需要一个免费函数。您可以将函数设置为
静态
,这意味着该函数无法在汽车上运行。然后 glut 就可以调用它,但是我假设你想操纵一辆汽车。解决方案是让函数将其调用传递给一个对象,如下所示:其中
activeCar
是一些全局可访问的汽车指针。您可以使用某种CarManager
单例来完成此操作。CarManager 会跟踪正在控制的活动汽车,因此当按下某个键时,您可以将其传递:
CarManager::reference().active_car().KeyPress(key, x, y)
。单例是一种只有一个全局可访问实例的对象。这超出了答案的范围,但您可以通过 Google 搜索有关创建答案的各种资源。查找 Meyers Singleton 以获得简单的单例解决方案。
另一种方法是使用一种 InputManager 单例,并且该管理器将跟踪它应通知按键按下的对象列表。这可以通过几种方式来完成,最简单的是这样的:
当然,如果这没有意义,请随意提出更多问题。
Your function is a member of a class. When you do something like
Car c; c.drive()
, thatdrive()
function needs a car to work with. That is thethis
pointer. So glut can't call that function if it doesn't have a car to work on, it's expecting a free function.You could make your function
static
, which would mean the function does not operate on a car. glut will then be able to call it, however I assume you want to manipulate a car. The solution is to make the function pass it's call onto an object, like this:Where
activeCar
is some globally accessible pointer to car. You can do this with some sort ofCarManager
singleton.CarManager keeps track of the active car being controlled, so when a key is pressed you can pass it on:
CarManager::reference().active_car().KeyPress(key, x, y)
.A singleton is an object that has only one globally accessible instance. It is outside the scope of the answer, but you can Google for various resources on creating one. Look up Meyers Singleton for a simple singleton solution.
A different approach is to have a sort of InputManager singleton, and this manager will keep track of a list of objects it should notify of key presses. This can be done in a few ways, the easiest would be something like this:
Of course feel free to ask more questions if this doesn't make sense.
我最终做的是
添加:
到 WorldObject 类
并将事件冒泡到 Car。
What I ended up doing was
Adding:
to the WorldObject class
And bubble the event to the Car.