Jump to content
  • Advertisement
Sign in to follow this  
Grappler

Method Pointer Troubles

This topic is 3712 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
Advertisement
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
The error message states what's wrong. The following is a signature of free function.
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!