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
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.