Hi I'm trying to create an array of function pointers contained within a class, the C++11 way. Can I have some help with this as I'm getting a bit mixed up. Here's what I have done so far. They are made up classes to define the problem.
#include <iostream>
#include <functional>
#include <vector>
using namespace std;
class CPerson;
class CAle
{
public:
CAle(void) {};
~CAle(void) {};
bool Sip1(CPerson* pPerson1, CPerson* pPerson2) { cout << "Sip 1" << endl; };
bool Sip2(CPerson* pPerson1, CPerson* pPerson2) { cout << "Sip 2" << endl; };
bool Sip3(CPerson* pPerson1, CPerson* pPerson2) { cout << "Sip 3" << endl; };
bool Sip4(CPerson* pPerson1, CPerson* pPerson2) { cout << "Sip 4" << endl; };
bool Sip5(CPerson* pPerson1, CPerson* pPerson2) { cout << "Sip 5" << endl; };
};
enum enSIPS
{
enSip1 = 0,
enSip2,
enSip3,
enSip4,
enSip5,
enSipCount
};
class CPerson
{
public:
CPerson(void) {};
~CPerson(void) {};
typedef std::function<bool(CPerson*, CPerson*)> FUNCTIONPOINTER;
vector<FUNCTIONPOINTER> fp_Sip;
};
int main(void)
{
CAle Ale;
CPerson Person;
Person.fp_Sip.push_back(mem_fn(&CAle::Sip1));
Person.fp_Sip.push_back(mem_fn(&CAle::Sip2));
Person.fp_Sip.push_back(mem_fn(&CAle::Sip3));
Person.fp_Sip.push_back(mem_fn(&CAle::Sip4));
Person.fp_Sip.push_back(mem_fn(&CAle::Sip5));
CPerson* pP1, *pP2;
for (auto it = Person.fp_Sip.begin(); it != Person.fp_Sip.end(); ++it)
(*it)(pP1, pP2); // <== Error No instance of function
Person.fp_Sip.at(enSip3)(pP1, pP2); // <== Error No instance of function
cout << "End of program...!";
cin.get();
return 1;
};
Errors:
c:\program files (x86)\microsoft visual studio 11.0\vc\include\xrefwrap(462): error C2440: 'newline' : cannot convert from 'CPerson *' to 'CAle *'
and more