Method Pointer Troubles
I'm trying to create a system similar to Quake's key binding system. I have a map that uses an int as the key value (for whatever input), and a class called GameControl as the stored value. GameControl is a class that stores a pointer to a void method, along with a few other useful things. A call to a Bind method creates an entry in the map with the specified input value and GameControl. When the game updates, it iterates through the map, checking the input and executing any of the methods binded to the keys being pressed.
Now here's my problem: I pass the method pointer to the constructor of the GameControl object, like so:
GameControl(&Game::Quit, ...)
Note that this is called from inside the Game class. This produces a compiler error:
no matching function call to `GameControl::GameControl(void (Game::*)(), ...)'
candidates are: GameControl::GameControl(const GameControl&)
GameControl::GameControl(void (*)(), ...)
It seems to me that the problem is with the Game:: part of it. However, if I take that out, then the compiler gives me an error and tells me that it should be there. I really have no idea how to fix this, does anybody have an idea?
Without seeing more code, I'm guessing you're trying to pass a pointer to a non-static member function, when then function doesn't expect that.
You can either change your function, or better, use Boost::Bind
You can either change your function, or better, use Boost::Bind
Quote:Original post by SwitchIn
Without seeing more code, I'm guessing you're trying to pass a pointer to a non-static member function, when then function doesn't expect that.
You can either change your function, or better, use Boost::Bind
Even better, use Boost.Bind alongside Boost.Function or (even better still!) Boost.Signal. There's a link to boost in my signature.
Thanks SwitchIn, you were right about it. A quick search of what you posted led me to a solution.
As for the Boost library, I'll have to give it a try sometime, it sounds like it'd make things a lot easier.
As for the Boost library, I'll have to give it a try sometime, it sounds like it'd make things a lot easier.
The error message states what's wrong. The following is a signature of free function.
Rest of implementation details have been discussed too often, google on member function pointers should cover all of that.
GameControl::GameControl(void (*)(), ...)
Signature of member function would be:void (T::*)()
Boost bind gets around this by allowing you to bind both, member and free functions to same signature.Rest of implementation details have been discussed too often, google on member function pointers should cover all of that.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement