我想把
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;
template<typename OtherDerived>
MyVectorType(const Eigen::MatrixBase<OtherDerived>& other) :
Eigen::Matrix<double, 3, 1, Eigen::DontAlign>(other) {
}
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;
}
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;
}
};
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()
,之间必须使用相同的接口
MyVectorType
和
MyRef
. 所以我想用
Interface
班级。
问题是:这个代码不能编译,因为我不能调用
rows()
里面
米雷夫
,所以我不知道如何访问
肌电型
或中的基础数据
Ref
类来调用其他方法。
我尝试过
derived()
但不起作用。我看了源代码,但我不明白
裁判
可正常使用所有接口
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;
^~~~