简体   繁体   中英

c++ passing non-templated eigen vector as function argument and modifying it in the function

I am implementing a function member of a derived class (declared as virtual in the base class). One of the arguments is a vectorXd and it is where the result of an operation will be stored.

I read the "Writing Functions Taking Eigen Types as Parameters" ( http://eigen.tuxfamily.org/dox/TopicFunctionTakingEigenTypes.html ) and their hack relies on templated functions, focusing in a generic Eigen object as possible parameter. In my case I don't think it'll work because it seems that you can't mix virtual and template.

On the other hand, I know that my argument is always going to be of type VectorXd, and I can even resize it to the right size before going into the function so that no resizing is necessary inside the function. I am trying the approach of passing the vector by reference as a const and using const_cast to be able to make the modifications I need, but I still get a linking error:

error LNK2001: unresolved external symbol "public: virtual void __thiscall problem::f(class method *,class Eigen::Matrix const &,class Eigen::Matrix const &)" (?f@problem@@UAEXPAVmethod@@ABV?$Matrix@N$0?0$00$0A@$0?0$00@Eigen@@1@Z)

Here, 'problem' is the base class, 'f' is the function, and 'method' is another class (I need an object of that type inside my function too).

In the definition of problem I have:

virtual void f(method *m, const Eigen::VectorXd &x, Eigen::VectorXd const &y);

In the definition of derived:problem I have:

void f(method *m, const Eigen::VectorXd &x, Eigen::VectorXd const &y) {...};

'y' is where I need to store the result. Any suggestions on how to achieve this? Thanks in advanced.

If your base class is abstract, set the method to =0 and be pure virtual .

If the base class is not abstract, it may not need that method. Do not include it in the base class.

If the base class is not abstract, but you need to be able to perform that operation on every instance of the base class as well as on derived, you need to implement the function in the base class as well as derived.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM