Operator() as functor in class


Please read tips for efficient and successful posting and posting code

_ROOT Version:6.22
Platform: Not Provided
Compiler: Not Provided


Hi,
i need to use class and operator() rather than function as bellow:
original code:

double RosenBrock(const double *xx )
{
  const double x = xx[0];
  const double y = xx[1];
  const double tmp1 = y-x*x;
  const double tmp2 = 1-x;
  return 100*tmp1*tmp1+tmp2*tmp2;
}

Which is input in the ROOT::Math::Functor f(&RosenBrock, 2);, means that it has 2 parameters. When I changed the function to class, I got an error.

class  RosenBrock {
 public:
    double xx;

   double operator() (double *xx ) {
      // function implementation using class data members
      const double x = xx[0];
      const double y = xx[1];
      const double tmp1 = y-x*x;
      const double tmp2 = 1-x;
      return 100*tmp1*tmp1+tmp2*tmp2;
   };
}

RosenBrock  r;
ROOT::Math::Functor f(r, 2);
In module 'MathCore':
/home/sima/Products/root/include/Math/Functor.h:120:14: error: no matching function for call to object of type '__cling_N524::RosenBrock'
      return fFunc(x);
             ^~~~~
/home/sima/Products/root/include/Math/Functor.h:90:4: note: in instantiation of member function
      'ROOT::Math::FunctorHandler<ROOT::Math::Functor,
      __cling_N524::RosenBrock>::DoEval' requested here
   FunctorHandler(unsigned int dim, const Func & fun ) :
   ^
/home/sima/Products/root/include/Math/Functor.h:428:17: note: in instantiation of member function
      'ROOT::Math::FunctorHandler<ROOT::Math::Functor,
      __cling_N524::RosenBrock>::FunctorHandler' requested here
      fImpl(new FunctorHandler<Functor,Func>(dim,f) )
                ^
input_line_55:8:21: note: in instantiation of function template specialization
      'ROOT::Math::Functor::Functor<__cling_N524::RosenBrock>' requested here
ROOT::Math::Functor f(r, 2);
                    ^
input_line_52:16:11: note: candidate function not viable: 1st argument ('const double *') would lose const
      qualifier
   double operator() (double *xx ) {
          ^

Do you have any idea to fix it?
thanks in advance

Hi,

In your Rosenbrock class define the member function taking const double * instead of double *:

double operator() (const double *xx ) {
 // function implementation using class data members
......

Lorenzo

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.