c++ pointer to non-static member functions

The problem is that you're trying to pass a pointer to a member function while a pointer to either a non-member function or a static member function is expected. And those are different types.

Here "fun" is a pointer to function: double(*fun)(double).

And here it's a pointer to a member function of class Funcs: double(Funcs::*fun)(double)

So here's how you can modify your code to make it work.

class Funcs
{
  // all the rest is the same
  double aaa(double(Funcs::*fun)(double), double x0);
};

double Funcs::solver(double X0)
{
  // ...
  result = aaa(&Funcs::Fun1, X0);
  // ...
}

double Funcs::aaa(double(Funcs::*fun)(double), double x0)
{
  return (this->*fun)(x0);
}

See live example at Coliru.

This may be a fine way to go if you want to deliberately limit your method aaa to accept only Funcs member functions as fun. If you'd also like to pass the non-member functions or e.g. lambdas to aaa, consider using std::function instead.


Your problem is that pointer-to-function is not the same as pointer-to-member-function. Read this for more info and this for clean way to write pointer-to-member-functions. If you need to keep this structure, make aaa a static or non-member function that takes everything it needs as arguments, or change the first argument of aaa to take a double(Funcs::*)(double) rather than a double(*fun)(double). You can even overload aaa to support both uses.


Actually Funcs::Fun1 is not double(*)(double).

Any non-static method has this signature: return_type(*)(class_type* this, arguments...)

Lets look at exact types:

//first argument of `aaa` has type double(*)(double)
double aaa(double(*fun)(double), double x0);

double Funcs::Fun1(double X) {
  double f1 = a*X;

  return f1;
}

// Fun1 has type double(Funs::*)(double)
// i.e it's a method of Funs and it takes (implicitly) first argument
// which is `this` pointer

// so that how `aaa` must  look like
double aaa(double(Funs::*fun)(double), double x0)
{
    // special sytax
    *this.*fun(x0);
}

// and that how `solver` looks like
double Funcs::solver(double X0)
{
  double result;

  // get pointer to method
  result = aaa(&Funs::Fun1, X0);
  return result;
}

If you are not familar with pointers to methods - check this