This forum has been archived. All content is frozen. Please use KDE Discuss instead.

Error in Inheriting Eigen::Vector3d

Tags: None
(comma "," separated)
coolcat
Registered Member
Posts
20
Karma
0
OS

Error in Inheriting Eigen::Vector3d

Wed Jul 04, 2012 9:53 am
Hi,
Though it sound stupid, I like to define new vector entity (called Vector3D) which has default constructor to initialize the memory to zero unlike Eigen::Vector3d.
Following the guideline of Eigen documentation, I defined the new class

#define EIGEN_USE_MKL_ALL
#include "Eigen/Dense"

class Vector3D : public Eigen::Vector3d
{
public:
typedef Eigen::Vector3d Base;
template<typename OtherDerived>
Vector3D& operator= (const Eigen::MatrixBase<OtherDerived>& other)
{
this->Base::operator=(other);
return *this;
}

Vector3D() : Eigen::Vector3d(Eigen::Vector3d::Zero()) { }
Vector3D(double x, double y, double z) : Eigen::Vector3d(x, y, z) { }
};

And in main.cpp such as

using namespace Eigen;
int _tmain(int argc, _TCHAR* argv[])
{
Vector3D v; // v = [0, 0, 0];
Vector3D w(4, 5, 6);

v << 1, 2, 3;
Vector3D u = v + w; // ERROR HERE!!!!!

return 0;
}

When I compile this, I get an error
1>e:\projects\roboticslab\roboticslab2\dev\tests\eigen_rmath\eigen_rmath\eigen_rmath.cpp(27): error C2440: On initializing: Cannot convert 'const Eigen::CwiseBinaryOp<BinaryOp,Lhs,Rhs>' to 'rMath::Vector3D'.
1> with
1> [
1> BinaryOp=Eigen::internal::scalar_sum_op<double>,
1> Lhs=const Eigen::Matrix<double,3,1>,
1> Rhs=const Eigen::Matrix<double,3,1>
1> ]
...

I know what the error means. But I wonder why, because it seems that the following code should take care of this conversion.
template<typename OtherDerived> Vector3D& operator= (const Eigen::MatrixBase<OtherDerived>& other) { this->Base::operator=(other); return *this; }

Where am I wrong?

When I add the constructor of the form
Vector3D(const Eigen::Vector3d& other) : Eigen::Vector3d(other) { }
to the class definition, it entails the unnecessary temporary of type Eigen::Vector3d to convert from CwiseBinaryOps.

How can I solve this problem?
jitseniesen
Registered Member
Posts
204
Karma
2
The operator= member function takes care of assignments, but in your main program, the line with the error is not an assignment. In C++, the line
Code: Select all
 u = v + w;
is an assignment which calls operator=, but
Code: Select all
 Vector3D u = v + w;
is a definition which calls the constructor.

So you need to add a constructor to your class. My guess is that the following will do the trick:
Code: Select all
class Vector3D : public Eigen::Vector3d
{
  /* ... */
  template<typename OtherDerived>
  Vector3D(const Eigen::MatrixBase<OtherDerived>& other)
    : Eigen::Vector3d(other)
  { }
};
coolcat
Registered Member
Posts
20
Karma
0
OS
That also eliminated creating unnecessary temporary.
Thanks..
;D ;D


Bookmarks



Who is online

Registered users: Baidu [Spider], Bing [Bot], Google [Bot]