0

Inside my class called AID, I have got three fitting curve functions: LastStateFitting, LinearCurveFitting and QuadCurveFitting. Another function choose which one is the most appropriate. I have stored pointer method inside a map (boost::unordered_map):

typedef enum predMethod{LAST, LINEAR, QUAD} predMethod;
    predMethod method;
typedef double*(AID::*fitting)(double[3]);
    double * LastStateFitting(double x[3]);
    double * LinearCurveFitting(double x[3]);
    double * QuadCurveFitting(double x[3]);
    const boost::unordered_map<predMethod, fitting> fittingMap = map_list_of
        (LAST, &AID::LastStateFitting)
        (LINEAR, &AID::LinearCurveFitting)
        (QUAD, &AID::QuadCurveFitting);

I don't understand how I can those functions. For now, I have done:

for ( int fit = LAST; fit != QUAD; fit++ )
{
    predMethod pm = static_cast<predMethod>(fit);
    double pred[3] = (*fittingMap.at(pm))(x);

But this gives me:

error: indirection requires pointer operand ('mapped_type' (aka 'double (AID::)(double *)') invalid) double pred[3] = (*fittingMap.at(pm))(x);

Thus my question is: how can one call a method pointer inside a map?

EDIT: a few post around a method pointer are already answered (here or here). But here, it still doesn't work because of the map container.h

Community
  • 1
  • 1
guhur
  • 2,500
  • 1
  • 23
  • 33
  • Upgrade to C++11, use `std::function` and [lambda](http://en.cppreference.com/w/cpp/language/lambda) – Basile Starynkevitch Feb 04 '16 at 15:48
  • I am not sure that a lambda can be applied here. I have written: `function f = [](double* x)->double*{return (this->*fittingMap.at(pm))(x)};` and my compiler returns "error: 'this' cannot be implicitly captured in this context" – guhur Feb 04 '16 at 15:58
  • the solution was actually to do: `fitting f = this->fittingMap.at(pm); double pred[3]; copy3((this->*f)(x), pred);` where copy3(a,b) copies the values of a inside b. – guhur Feb 04 '16 at 18:25

0 Answers0