c++ - Missing call to bound pointer to member function -


i trying implement generic event handling system templates, error @ compile time when try add function list.

error c2298: missing call bound pointer member function

class eventmanager { public:     template <class object>     void subscribe(std::string eventname, object *obj, void(object::*newevent)(event))     {         (obj->*newevent)(event()); // can call event fine if execute alone         abc.push_back(obj->*newevent); // error c2298 here     }      static eventmanager* get();  private:     std::vector<void(*)(event)> eventslist; }; 

and call it:

eventmanager::get()->subscribe("eventname", this, &testclass::eventtest);  void atidemanager::eventtest(event event) {     //test } 

i quite new c++ , templates, i'm sure solution easy, don't understand why can call event not add vector. know how possibly that?

as documentation error message says:

a pointer member-function expression must call member function.

in other words, cannot store pointer member-function including object on call it, later use.

here simpler example using names similar yours:

struct object {     void f() {} };  int main() {     object obj;      using member_function_pointer = void (object::*)();       member_function_pointer ptr = &object::f;      (obj.*ptr)();     (obj.*ptr); // error } 

and makes lot of sense. abc vector of pointers member-function (or vector of function pointers); cannot magically store object along each pointer in it.

generally, pointers member functions not nicest, cleanest or best feature of c++, , awful syntax renders every piece of code using unreadable. fortunately, c++11 introduced std::function , lambdas, gives better alternative:

#include <functional> #include <iostream> #include <string> #include <vector>  struct event {     int i; };  class eventmanager { public:     void subscribe(std::string const& eventname,                    std::function<void(event const&)> event)     {         abc.push_back(event);     }      static eventmanager& get()     {         static eventmanager instance;         return instance;     }      void onevent(event const& event)     {         (auto&& f : abc)         {             f(event);         }     }  private:     eventmanager() {}     eventmanager(eventmanager const&) = delete;     eventmanager& operator=(eventmanager const&) = delete;      std::vector<std::function<void(event const&)>> abc;  };  struct atidemanager {     void f()     {         eventmanager::get().subscribe("eventname",             [=](event const& event) { eventtest(event); });     }      void eventtest(event const& event)     {         std::cout << "eventtest: " << event.i << "\n";     } };  int main() {     atidemanager manager;     manager.f();     event some_event{ 123 };     eventmanager::get().onevent(some_event); } 

i've fixed needless use of pointers references can used, , const-correctness issues.


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -