Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/163.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;特征值:使用简单操作在转换中获得子类向量错误_C++_Operator Overloading_Subclass_Eigen - Fatal编程技术网

C++ C++;特征值:使用简单操作在转换中获得子类向量错误

C++ C++;特征值:使用简单操作在转换中获得子类向量错误,c++,operator-overloading,subclass,eigen,C++,Operator Overloading,Subclass,Eigen,我将Eigen::Vector2d子类化,以获得一些我不在这里编写的方便方法(如MyVec.randomize(),MyVec.distanceWithThreshold(),等等) 但是当我尝试给一个新的向量分配一些简单的操作时,我在转换中遇到了一个错误。让我们看看我的代码: #include <iostream> #include "Eigen/Core" class Vec2D : public Eigen::Vector2d { public: Ve

我将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;
}
#包括
#包括“特征/核心”
Vec2D类:公共特征::Vector2d{
公众:
Vec2D():Eigen::Vector2d(){};
Vec2D(双x,双y):本征::向量2D(x,y){
};

STD::关于编译错误,C++和C++中的构造函数和赋值运算符不会自动继承。看这个。特别是,您必须重新实现Matrix类的所有构造函数,并添加:

using Vector2d::operator=; 
转发赋值运算符

关于A-B,它的类型是CwiseBinaryOp表达式。如果您希望它是一个Vec2D,那么您必须将其转换为:

cout << Vec2D(A-B);

难道你还没有从
cwisebaryaryop…
实现构造函数吗?你在
操作符中错过了
const
,这是一个更好的方法,但是这个简单的“添加构造函数”对我来说很有用!将其添加为答案“添加构造函数”已经是@ggael answer的一部分,但他的答案更完整。是的,我读得更好,它很好地解释了一切!好的,很清楚。无论如何,我不理解使用Vector2d::operator=
cout << Vec2D(A-B);