My question is similar to this one . And the answer is "Karek S.B." really helped me. I have the following classes:
Base.h:
class Base{ public: Base(){} virtual ~Base(){} virtual void init() = 0; };
A1.h:
#include <iostream> #include "Base.h" using namespace std; class A1 : public Base{ public: A1(){} virtual ~A1(){}; virtual void init(){ cout << "A1::init() called" << endl; } void f1(){ cout << "Im in A1::f1" << endl; } void f2(int val){ cout << "Im in A1::f2 with val: " << val << endl; } };
I have another class that should be able to store any common member function with any type and number of arguments. The class looks something like this:
MFholder.h:
#include <functional> #include <deque> using namespace std; class MFHolder{ public: MFHolder(){}; ~MFHolder(){}; template<typename T, typename R, typename ... Args> R addMF(T & obj, R (T::*pf)(Args ...), Args&& ... args){ mfp.push_back(function<void()>(bind(pf, &obj, forward<Args>(args) ...))); } void runTasks(){ while(!mfp.empty()){ auto task = mfp.front(); mfp.pop_front(); task(); } } private: deque< function<void()> > mfp; };
Now I want to add some member function to MFHolder from main: main.cpp:
#include "A1.h" #include "MFHolder.h" int main(){ MFHolder mfh; A1 a1Obj;
When compiling my code, the following error occurs.
there is no corresponding function to call 'MFHolder::addMF(A1&, void (A1::*)(int), int&)'
And the candidate:
template<class T, class R, class ... Args> R MFHolder::addMF(T&, R (T::*)(Args ...), Args&& ...)
thanks in advance!:)
source share