Connecting signals using a member function

Started by crdl_pls, 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:'&': illegal operation on bound member function expression

Here is the minimal context:

// 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:

// gameEngine.h
class gameEngine {

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

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.


It should be "&GameEngine::Quit" instead of "&(game->Quit)".

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


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.

buttonQuit->connect("clicked", &gameEngine::Quit, game);

is correct. Thanks.