Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
What's wrong with this?

C++
#include <iostream>
#include <string>
#include <queue>

using namespace std;

class ListenerTest;

class IListener {
};

template<typename ...T>
class Event {
public:
};

template<typename ...T>
class EventListener {
public:
    typedef void (ListenerTest::* memberFunc) (Event<T...> event);

    EventListener<T...>(memberFunc f) : f(f) {
    }
    void trigger(Event<T...> event) {
        f(event); // - - - - ERROR - - - -
    }
private:
    memberFunc f;
};

template<typename ...T>
class EventHandler {
public:
    EventHandler<T...>() {
    }
    void addListener(EventListener<T...>& listener) {
        this->listeners.push_back(listener);
    }
    void removeListener(EventListener<T...>& listener) {
        this->listeners.erase(std::remove(this->listeners.begin(), this->listeners.end(), listener), this->listeners.end());
    }
    void fire(Event<T...>& event) {
        for (EventListener<T...> listener : this->listeners) {
            listener.trigger(event);
        }
    }
private:
    vector<EventListener<T...>> listeners;
};

class ListenerTest : IListener {
public:
    void triggerTest(Event<int, int> event) {
        cout << "triggerTest" << endl;
    }
};



int main() {
    auto pia = &ListenerTest::triggerTest;

    ListenerTest lTest;

    Event<int, int> myEvent;
    EventHandler<int, int> handlerTest;
    EventListener<int, int> listenerTest(pia);

    handlerTest.addListener(listenerTest);
    handlerTest.fire(myEvent);
}


I'm sorry, it's hard to explain the whole, thing so if you could just run the code (in your wise brain?) and explain me how to get rid of the error, you'll make my day.

- error C2064: term does not evaluate to a function taking 1 arguments (line 25).
Posted
Updated 6-Jun-14 3:58am
v2
Comments
Sergey Alexandrovich Kryukov 6-Jun-14 10:01am    
Do you suggest to count like to find the one showing error? :-)
—SA
Sergejack 6-Jun-14 10:15am    
No, I just can't find word to explain all that should be going on there. I would just mess up the question. Let alone, English isn't my mother language. XD
Sergey Alexandrovich Kryukov 6-Jun-14 10:44am    
Just add a comment to the line giving the compilation error...
—SA
Member 10869098 6-Jun-14 12:52pm    
It's actually already there. f(event); // - - - - ERROR - - - -
Sergey Alexandrovich Kryukov 6-Jun-14 18:40pm    
All right, thank you...
—SA

f is a pointer (to a member function), so I had to call it using *f.
And since it's a (pointer to a) member function, it need an instance to act upon.

Plus "event" seems like a reserved word, so I guessed I should change it to somehting else, ie: evt.

so the correct syntax was.

C++
this->instance->*f(evt); // instance being a new member of ListenerTest type
 
Share this answer
 
Comments
Stefan_Lang 10-Jun-14 6:31am    
That solution is backwards - where do you get the instance from? And why should the EventListener class assume that the function pointer passed to it is instead an abstract reference to a class method table?

Like you said, to get a real function pointer from a class you need an instance first! Your EventListener implementation was correct, you just passed the wrong kind of argument.
Stefan_Lang 10-Jun-14 6:36am    
As for "event" being a keyword - it appears ot be only so for managed C++/CLI. That's why VS colors it blue. But for native C++ it is not a keyword.
C++
    typedef void (ListenerTest::* memberFunc) (Event<t...> event);
</t...>

You have defined a pointer to nothing.
 
Share this answer
 
Comments
Member 10869098 6-Jun-14 12:59pm    
Then what's the correct syntax to that typedef? I want a pointer to a member function which returns nothing and take a Event<t...> as parameter.
Richard MacCutchan 7-Jun-14 5:00am    
Then you need to include the name of the member function. As it stands the compiler has no idea what function your pointer may be referring to.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900