我已将 Eigen::Vector2d 子类化为有一些我不会在这里写的方便方法(如MyVec.randomize()
,MyVec.distanceWithThreshold()
等)。
但是当我尝试为新向量分配一些简单的操作时,我遇到了转换错误。让我们看看我的代码:
#include <iostream>
#include "Eigen/Core"
class Vec2D : public Eigen::Vector2d {
public:
Vec2D() : Eigen::Vector2d() {};
Vec2D(double x, double y) : Eigen::Vector2d(x,y) {}
};
std::ostream& operator<< (std::ostream &out, Vec2D &cPoint) {
out << "(" << cPoint.x() << ", " << cPoint.y() << ")";
return out;
}
int main() {
// test with base class
Eigen::Vector2d A = Eigen::Vector2d(0,0);
Eigen::Vector2d B = Eigen::Vector2d(5,5);
Eigen::Vector2d C = A - B;
std::cout << C.x() << " " << C.y() << std::endl;
// test with my subclassed Vec2D
Vec2D _A,_B;
_A = Vec2D(0, 0);
_B = Vec2D(5, 5);
std::cout << _A-_B << std::endl; // my stream overload is not called
std::cout << _A << std::endl; // my stream overload is called
// now the problem
Vec2D dudeee = _A - _B; // if I comment this line, no error
std::cout << dudee << std::endl; // if I comment this line, no error
return 0;
}
错误是:
test.cpp: In function 'int main()':
test.cpp:34: error: conversion from 'const Eigen::CwiseBinaryOp<Eigen::internal::scalar_difference_op<double>, const Eigen::Matrix<double, 2, 1, 0, 2, 1>, const Eigen::Matrix<double, 2, 1, 0, 2, 1> >' to non-scalar type 'Vec2D' requested
test.cpp:35: error: 'dudee' was not declared in this scope
我认为(因为调用我的流运算符重载)我必须以某种方式覆盖 Vec2D 中的普通运算符(+、-*、/),也以某种方式分配一个新的 Vec2D 对象,但我没有知道该怎么做。一些忠告?