Question

I am trying to store a member function from an object to call it when an appropriate event happens. Unfortunately this code gives a lot of error messages. Can anybody help me?

class MainPage
{
public:

    MainPage::MainPage(void)
    {
        image1 = new STImagePanel("Untitled.png");
        image1->OnPointerPressedHandler->add(&MainPage::imageMousePressed,this);
    }

    void MainPage::imageMousePressed(STObject* sender, STPointerEventArg * args)
    {
        MainPage * dsda = (MainPage*)this;
        pres = true;
    }

};


template<class SENDERTYPE, class ARG>
struct callbackInfo
{
    boost::function<void(SENDERTYPE, ARG)> func;
};

template<class SENDERTYPE, class ARG>
class STEventHandler
{
public:
    template<class T, class P>
    void add(T x, P that)
    {
        //The following line gives the error
        callbackInfo<SENDERTYPE,ARG> tmp { boost::bind(x,that,_2)};  
        functions->addLast(tmp);
    }

private:
    STLinkedList<callbackInfo<SENDERTYPE,ARG>>* functions;
}

This gives the following errors:

http://pastie.org/8014181

Update: with the following correction it works fine:

template<class T, class P>
void add(T x, P that)
{
    boost::function<void(SENDERTYPE,ARG)> func(boost::bind(x,that,_1,_2));
    callbackInfo<SENDERTYPE,ARG> tmp;
    tmp.func = func;
}
Was it helpful?

Solution

One problem that I see MainPage::imageMousePressed(STObject* sender, STPointerEventArg * args) has two arguments, but in the call to boost::bind, you're only binding _2 to sender. That's doesn't work. You have to either bind both, or pass NULL or some sentinel value.

More generally, it looks to me like you're reinventing boost::signals

OTHER TIPS

Two suggestions:

1) In code you did not show the first error message may have prevented some header inclusion;

2) You lack a semi-colon at the end of your class MainPage definition.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top