Reputation: 31
I have a the following member function:
void GClass::InitFunctions()
{ // Initialize arrays of pointers to functions.
ComputeIntLen[0] = &ComputeILS;
ComputeIntLen[1] = &ComputeILE;
ComputeIntLen[2] = &ComputeILI;
PostStep[0] = &PSS;
PostStep[1] = &PSE;
PostStep[2] = Ψ
gRotation = new Rotation();
}
GClass obviously contains all the relevant members -:
void ComputeILE(Int_t, Int_t *, Double_t *);
void ComputeILI(Int_t, Int_t *, Double_t *);
void PSS(Int_t , Int_t *, Int_t &, Int_t*);
void PSE(Int_t, Int_t *, Int_t &, Int_t*);
void PSI(Int_t , Int_t *, Int_t &, Int_t*);
ComputeIntLenFunc ComputeIntLen[gNproc];
PostStepFunc PostStep[gNproc];
... //other members
}
where gNproc is a global const int and ComputeIntLenFunc and PostStepFunc are typedefs like this:
typedef void (*ComputeIntLenFunc)(Int_t ntracks, Int_t *trackin, Double_t *lengths);
typedef void (*PostStepFunc)(Int_t ntracks, Int_t *trackin, Int_t &nout, Int_t* trackout);
When I Compile this I get gcc gives an error: "ISO C++ forbids taking the address of an unqualified or parenthesized non-static member function to form a pointer to member function. Say ‘&GClass::ComputeIntLenScattering’ "
When I replace FunctionNames by GClass::FunctionNames in InitFunctions() I get "cannot convert ‘void (GClass::*)(Int_t, Int_t*, Double_t*)’ to ‘void (*)(Int_t, Int_t*, Double_t*)’ in assignment"
Please help me. What topic of C++ is this ?
Upvotes: 3
Views: 4575
Reputation: 361482
Non-static member function pointer is different from free function pointer. What you're using is basically free function pointer types, which would not work, as type of &GClass::ComputeILS
is incompatible with ComputeIntLenFunc
.
Use this:
typedef void (GClass::*ComputeIntLenFunc)(Int_t, Int_t *, Double_t *);
typedef void (GClass::*PostStepFunc)(Int_t, Int_t *, Int_t &, Int_t*);
I omitted the parameter names, as they're not required in the typedefs.
Also, you've to use GClass::
when you get the address of member functions:
ComputeIntLen[0] = &GClass::ComputeILS;
ComputeIntLen[1] = &GClass::ComputeILE;
ComputeIntLen[2] = &GClass::ComputeILI;
PostStep[0] = &GClass::PSS;
PostStep[1] = &GClass::PSE;
PostStep[2] = &GClass::PSI;
The error message says this very clearly:
ISO C++ forbids taking the address of an unqualified or parenthesized non-static member function to form a pointer to member function. Say ‘&GClass::ComputeIntLenScattering
Upvotes: 2
Reputation: 1607
You need to prepend the class to your typedefs as well:
typedef void (GClass::*ComputeIntLenFunc)(Int_t ntracks, Int_t *trackin, Double_t *lengths);
typedef void (GClass::*PostStepFunc)(Int_t ntracks, Int_t *trackin, Int_t &nout, Int_t* trackout);
Then your (first) type will mean: this is a pointer to a member function in class GClass which takes an Int_t
, an Int_t*
and so forth whereas in your version it just referred to a free function with the same parameters.
Upvotes: 1