Connecting signals using a member function
« on: 11 March 2016, 00:39:52 »
I tried to follow your tutorial here on signals, but I seem to be making a mistake when trying to connect a  member function to a signal.
Error:
Code: [Select]
'&': illegal operation on bound member function expression
Here is the minimal context:
Code: [Select]
// stateMainMenu.cpp
void stateMainMenu::Init(gameEngine* game) {
tgui::Button::Ptr buttonQuit = std::make_shared<tgui::Button>();
buttonQuit->connect("clicked", &(game->Quit), &game); // Crash Here
gui.add(buttonQuit, "buttonQuit");
}

and the class definition for gameEngine:

Code: [Select]
// gameEngine.h
class gameEngine {

public:
//Other Stuff
bool Running() { return isRunning; }
void Quit() { isRunning = false; }

private:
bool isRunning;
};

So far I've tried small stuff I thought would work like making both isRunning and Quit static, or writing a small function in gameEngine.cpp to run game->quit, and then connect that function to the button. The last two work but I want to see if I can get the function pointers working. Any help would be amazing.

*

texus

  • *****
  • 1143
    • View Profile
    • Texus's Blog
Re: Connecting signals using a member function
« Reply #1 on: 11 March 2016, 00:42:27 »
It should be "&GameEngine::Quit" instead of "&(game->Quit)".

The "Connecting member functions" section in the tutorial shows an example of this.

Re: Connecting signals using a member function
« Reply #2 on: 11 March 2016, 00:49:26 »
Thanks, I guess I wasn't reading what was right in front of me. Also I just realised that &game is creating a pointer to a pointer to a gameEngine instance.

Code: [Select]
buttonQuit->connect("clicked", &gameEngine::Quit, game);
is correct. Thanks.