Page 1 of 1

Subscribing Events with Inherited classes

Posted: Fri Sep 05, 2008 00:22
by Kevin
Hello,

I have a class NurikabeGUI, which inherits from BoardGUI. Inside BoardGUI I have the function:

Code: Select all

bool BoardGUI::checkClicked(const CEGUI::EventArgs& args)
{
    ...
    return true;
}

I am trying to subscribe a button to this callback in the NurikabeGUI constructor. I first tried:

Code: Select all

_checkButton->subscribeEvent(CEGUI::PushButton::EventClicked, CEGUI::Event::Subscriber(&BoardGUI::checkClicked, this));

which gives me the errors:

Code: Select all

c:\Progs\PencilPuzzles\NurikabeGUI.cpp(24): error C2248: 'Penpa::BoardGUI::checkClicked' : cannot access protected member declared in class 'Penpa::BoardGUI'

Code: Select all

c:\Progs\PencilPuzzles\NurikabeGUI.cpp(24): error C2661: 'CEGUI::SubscriberSlot::SubscriberSlot' : no overloaded function takes 2 arguments

I then tried

Code: Select all

_checkButton->subscribeEvent(CEGUI::PushButton::EventClicked, CEGUI::Event::Subscriber(&NurikabeGUI::checkClicked, this));

since 'this' is an instance of NurikabeGUI. In this case, I get the second error as above, but not the first.

So I suppose my question is this: Is it possible to use an inherited function as a callback?

Thanks in advance!
Kevin

Re: Subscribing Events with Inherited classes

Posted: Fri Sep 05, 2008 16:01
by Jamarr
Kevin wrote:So I suppose my question is this: Is it possible to use an inherited function as a callback?

Yes, of course.

Code: Select all

c:\Progs\PencilPuzzles\NurikabeGUI.cpp(24): error C2661: 'CEGUI::SubscriberSlot::SubscriberSlot' : no overloaded function takes 2 arguments

This means your code is not written correctly. You probably messed up the decleration or something. If you search the forum for "no overloaded function takes arguments" (note I left out the '2' - it messes up the results page) every single one of those posts end up being syntax related.

Posted: Fri Sep 05, 2008 19:35
by Kevin
Hello,

Thanks for the reply, but I don't think that is the problem. I have successfully subscribed to many events before with no problems. The difference with this one is that the function I am trying to subscribe to is not declared in the class itself, but a class it inherits from, which seems to be what it's complaining about.

Kevin

Posted: Sun Sep 07, 2008 20:04
by Kevin
Just an update:

I tried cut-and-pasting the function exactly as it was from the base class (BoardGUI) to the derived class (NurikabeGUI), and now it works fine, so the problem is definitely to do with the fact that it's an inherited function.

Posted: Wed Sep 10, 2008 18:37
by Jamarr
Yes, I see. I missread your initial post. I thought you had already redefined the method in the derived class.

It is odd, because the CEGUI::SubscriberSlot class already has a templated constructor for class methods. For some reason the compiler is not recognizing inherited class methods as valid template parameters for that constructor.

You can still work around the issue by explicitly using the FunctorReference constructor instead, like so:

Code: Select all

_checkButton->subscribeEvent(CEGUI::PushButton::EventClicked, CEGUI::Event::Subscriber(CEGUI::MemberFunctionSlot<NurikabeGUI>(&NurikabeGUI::checkClicked, this)));