扩展特征参考类

时间:2019-02-04 19:29:33

标签: 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类。

问题:由于无法在rows()中调用MyRef,因此无法编译此代码,因此我不理解如何访问MyVectorType或其中的基础数据Ref类来调用其他方法。

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

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;
                ^~~~

1 个答案:

答案 0 :(得分:3)

InCallService类依赖于模板参数时,即使Base(假设)Derivedmember_base继承而来,仅使用Base member_base类,不等同于Derived

那是

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()