2

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

Daniel Casserly
  • 3,552
  • 2
  • 29
  • 60
Robin Nicole
  • 646
  • 4
  • 17
  • Did you try typedef? `typedef Doub type_for_template (VecDoub_I &)` and later `Powell powell(test.instance);` – Mohit Jain Mar 23 '15 at 08:34
  • @user2197372 1) Numerical-recipes are probably not the best way to go. 2) You look like you're calling an object of "Doub" - Do you reference this class? – Phorce Mar 23 '15 at 08:35
  • The class Doub are classes which are used in the numerical recipes. Doub -> double; Vec_Doub_I is a constant vector of doubles – Robin Nicole Mar 23 '15 at 08:37
  • Just curious : can someone explain the meaning of this syntactic madness please ? `Powell`. – Félix Cantournet Mar 23 '15 at 10:29
  • Powell is the class which I will use to minimise my function. this function takes a constant vecor as an argument a pointer to a constant vector of double which is typedef `VecDoub_I` in the nuerical recipes and the return type is `Doub`which is a typedef for double in the numerical recipes – Robin Nicole Mar 23 '15 at 23:09

1 Answers1

0

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);
Motti
  • 110,860
  • 49
  • 189
  • 262