In the following code I am getting the error:
a value of type (double*)(const double& arg) const cannot be assigned to an entity of type pt2calculateA
Any suggestions on how to make it work?
class myClass {
private:
typedef double (*pt2calculateA)(double);
pt2calculateA calculateA[2];
public:
myClass () {
calculateA[0] = &calculateA1; //->error
calculateA[1] = &calculateA2; //->error
}
double calculateA1(const double& arg) const {
...
}
double calculateA2(const double& arg) const {
...
}
}
myClass::calculateA1()is not a function; rather, it is a member function. So the types are naturally not compatible.The type of
&myClass::calculcateA1isdouble (myClass::*)(const double &) const, which is a pointer-to-member-function (PTFM). Note that you can only use a PTMF together with a pointer to an object instance (i.e. amyClass*).If you change your typedef, you could at least store the pointers correctly:
You’ll have to say
&myClass::calculateA1, etc., to take the address.In C++11, you can initialize the array in the initializer list: