Class method type

485 Views Asked by At

I am writing a code using the numerical recipes library and I would like to minimize a function which is actually the method of a class. I have this type of code:

class cl{
  Doub instance(VecDoub_I &x)
  {
    return x[0]*x[0] + x[1]*x[1];
  };
};

And I want to minimize this function using the Powell method, in the following code

// enter code here
int main(void)
{
  cl test;
  Powell<Doub (VecDoub_I &)> powell(test.instance);
}

But when I compile I get the following error :

main.cpp:241:22: error: invalid use of member function (did you forget the ‘()’ ?)
main.cpp:242:54: error: no matching function for call to ‘Powell<double(const NRvector<double>&)>::Powell(<unresolved overloaded function type>)’

Has anybody already ecountered this problem ? Thanks in advance

1

There are 1 best solutions below

0
Motti On

Since cl::instance is an instance method (i.e. not a static method) it requires a this pointer. Therefore you can't capture a pointer to it in a regular function pointer. Also in order to get the address of a function you should use the & operator.

I'm not familiar with the library you're using but I think changing the function to be static (or making it a free function) and adding the & should help.

Powell<Doub (VecDoub_I &)> powell(&cl::instance);