c++ - 扩展 Eigen Ref 类

标签 c++ class templates interface eigen

我正在尝试扩展 Ref Eigen 类以使用自定义类。我有以下代码:

#include <iostream>
#include <eigen3/Eigen/Dense>

class Interface {
public:
    virtual ~Interface() {
    }
    virtual void customMethod() const = 0;
};

class MyVectorType: public Eigen::Matrix<double, 3, 1, Eigen::DontAlign>,
        public Interface {
public:
    MyVectorType(void) :
            Eigen::Matrix<double, 3, 1, Eigen::DontAlign>() {
    }
    typedef Eigen::Matrix<double, 3, 1, Eigen::DontAlign> Base;
    // This constructor allows you to construct MyVectorType from Eigen expressions
    template<typename OtherDerived>
    MyVectorType(const Eigen::MatrixBase<OtherDerived>& other) :
            Eigen::Matrix<double, 3, 1, Eigen::DontAlign>(other) {
    }
    // This method allows you to assign Eigen expressions to MyVectorType
    template<typename OtherDerived>
    MyVectorType & operator=(const Eigen::MatrixBase<OtherDerived>& other) {
        this->Base::operator=(other);
        return *this;
    }
    virtual void customMethod() const {
        std::cout << rows() << std::endl;
    }
};

template<typename T, int Options>
class MyRef: public Eigen::Ref<typename T::Base, Options, Eigen::Stride<0, 0> >,
        public Interface {
public:
    typedef Eigen::Ref<typename T::Base, Options, Eigen::Stride<0, 0> > Base;
    template<typename Derived>
    MyRef(Eigen::DenseBase<Derived>& expr) :
            Eigen::Ref<typename T::Base, Options, Eigen::Stride<0, 0> >(expr) {
    }
    virtual void customMethod() const {
        std::cout << rows() << std::endl; // <-----error
    }
    EIGEN_INHERIT_ASSIGNMENT_OPERATORS(MyRef)};

template<typename T, int Options>
class MyRef<const T, Options> : public Eigen::Ref<typename T::Base, Options,
        Eigen::Stride<0, 0> >, public Interface {
public:
    template<typename Derived>
    MyRef(const Eigen::DenseBase<Derived>& expr) :
            Eigen::Ref<typename T::Base, Options, Eigen::Stride<0, 0> >(expr) {
    }
    virtual void customMethod() const {
        std::cout << rows() << std::endl; // <-----error
    }
};

void init(MyRef<MyVectorType, Eigen::Unaligned> m) {
    m.customMethod();
}

int main() {
    Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::AutoAlign, 12,
            12> mm(3, 1);
    Eigen::Map<MyVectorType::Base> map(mm.data(), 3, 1);
    MyRef<MyVectorType, Eigen::Unaligned> ref(map);
    init(ref);
    std::cout << mm << std::endl;
    return 0;
}

为了调用init() 等自定义方法,必须在MyVectorTypeMyRef 之间使用相同的接口(interface)。所以我想使用 Interface 类。

问题:此代码无法编译,因为我无法在 MyRef 中调用 rows(),所以我不明白如何访问 MyVectorTypeRef 类中的底层数据调用其他方法。

我尝试使用 derived() 进行访问,但它不起作用。我查看了源代码,但我不明白Ref 是如何与DenseBase 的所有接口(interface)正常使用的。我想为我的自定义方法做同样的事情。

Gcc 错误:

../main.cpp:49:16: error: there are no arguments to ‘rows’ that depend on a template parameter, so a declaration of ‘rows’ must be available [-fpermissive]
   std::cout << rows() << std::endl;
                ^~~~
../main.cpp:49:16: note: (if you use ‘-fpermissive’, G++ will accept your code, but allowing the use of an undeclared name is deprecated)
../main.cpp: In member function ‘virtual void MyRef<const T, Options>::customMethod() const’:
../main.cpp:63:16: error: there are no arguments to ‘rows’ that depend on a template parameter, so a declaration of ‘rows’ must be available [-fpermissive]
   std::cout << rows() << std::endl;
                ^~~~

最佳答案

Base类取决于模板参数,即使 Derived (假设)member_base继承自Base , 简单地使用 member_baseDerived类,不等于 this->member_base .

也就是

template<typename T>
class Base { public:    void member_base(); };

template<typename T>
class Derived : Base<T> 
{
public:
    void member_derived() 
    {
        member_base(); // calls external(global) member_base() or error
    }
};

在您的情况下,rows() 发生了什么与上述情况完全相同。

您需要使用 this-> 来获得资格或 Base<T>:: , 对于您从 Base 继承的所有成员.

你的情况

this->row() 

Eigen::Ref<typename T::Base, Options, Eigen::Stride<0, 0> >::rows()

关于c++ - 扩展 Eigen Ref 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54523063/

相关文章:

python - 在 python 中保存和加载类

c++ - 在 sf::Vector 中重载 operator< 时出错

c++ - 使用 decltype 给出警告引用局部变量

c++ - Qt 带 Cmake : set(QT_USE_QTWEBKIT TRUE) not working

C++:运算符和模板

c++ - 继承 Shell 类以供容器使用

java - 实现类的 Jython 脚本未从 Java 正确初始化

c++ - 将枚举类模板限制为标记类型

c++ - 是否保证浮点变量的拷贝按位相当于原始变量?

c++ - 我需要一个用于 boost::ublas 矩阵的虚拟析构函数吗?