c++ - 扩展 Eigen Ref 类
问题描述
我正在尝试扩展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()
之间必须使用相同的接口。所以我想用一个类。MyVectorType
MyRef
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;
^~~~
解决方案
当一个Base
类依赖于模板参数时,即使Derived
(比方说)member_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()
或者
Eigen::Ref<typename T::Base, Options, Eigen::Stride<0, 0> >::rows()
推荐阅读
- python-3.x - 如何解决不能在类似字节的对象上使用字符串模式
- linux - 从内核源代码中取出驱动程序代码并将其编译为树外模块
- html - 如何将图像的高度设置为与其宽度相同?
- python - 如何赋予 Python 对文件窗口的完全权限
- python - 我想播放一首歌,同时创建一个控制歌曲的 tkinter 窗口
- kotlin - 在 Intellij IDE 中编译 Kotlin 时无法删除缓存目录
- javascript - 如何编写 Jasmine 测试以覆盖 event.target.click
- django - 如何修复 django 模型实例
- html - 如何使用 TypeScript 换行
- javascript - 在 Jquery 或 Javascript 中模拟物理点击