Error while using connect function in class

Started by JerryBohenek, 28 April 2020, 17:36:26

JerryBohenek

Hello,

When I used connect in int main(), it worked fine, but after moving it to the class it doesn't work anymore.

The code won't compile and these error messages show up:
error C3867: 'PixelPaint::onExitClicked': non-standard syntax; use '&' to create a pointer to member
error C2672: 'tgui::SignalWidgetBase::connect': no matching overloaded function found

Code:

menu->connect("MenuItemClicked", onExitClicked, std::ref(window));

void onExitClicked(sf::RenderWindow& window, const std::string& item)
{
if (item == "Wyjscie") {
window.close();
}
}

texus

Class member functions aren't the same as free functions, they have an extra hidden "this" pointer that you also need to bind.

If "onExitClicked" is part of the PixelPaint class then the connect function should look like this:
Code (cpp) Select
menu->connect("MenuItemClicked", &PixelPaint::onExitClicked, this, std::ref(window));

Note that the "this" in the above code is only correct when this line of code is also placed inside the PixelPaint class. If the code is called from somewhere else the instead of "this" you should just pass the address of the PixelPaint object from which you want onExitClicked to be called.

JerryBohenek

Oh, that's right,

Thanks a lot and stay safe!  :D