I am a little bit new in C++ so I am not sure how to handle such a problem. I have a class with several private methods. Inside one of the methods, there is a functor from which I want to call another class method. Here is the example code:
# myprogram.h
namespace new_space
{
// Generic functor
template<typename _Scalar, int NX = Eigen::Dynamic, int NY = Eigen::Dynamic>
struct Functor
{
typedef _Scalar Scalar;
enum {
InputsAtCompileTime = NX,
ValuesAtCompileTime = NY
};
...
};
class myNewClass
{
private:
void f1();
void f2();
};
}
# myprogram.cpp
namespace new_space
{
void myNewClass::f1()
{
...
}
void myNewClass::f2()
{
struct my_functor : Functor<double>
{
my_functor(void): Functor<double>(3,3) {}
int operator()(const Eigen::VectorXd &x, Eigen::VectorXd &fvec) const
{
f1();
...
}
};
...
}
}
When I want to make this file I get the error:
cannot call member function ‘void new_space::myNewClass::f2()’ without object
Is there a way to fix this problem?
You need give your functor access to the this
pointer of the object creating it. Something like this:
void myNewClass::f2()
{
struct my_functor : Functor<double>
{
myNewClass *self;
my_functor(myNewClass& self): Functor<double>(3,3), self(&self) {}
int operator()(const Eigen::VectorXd &x, Eigen::VectorXd &fvec) const
{
self->f2();
...
}
};
// when creating the functor, do it like this:
// my_functor(*this)
};