【发布时间】:2014-03-29 01:30:51
【问题描述】:
我尝试编写一个 C++ 模板类,它应该能够通过相同的接口处理“简单”类型和类似“Eigen::MatrixBase”的类型。我设法用两种不同的简单类型获得了所需的行为,但很难将本征语法压缩到我的最小示例中......也许有人可以给我一个建议?
环顾四周,this 接近我想要的——没有 Eigen。 This 看起来也很相似。
#ifndef __MINIMAL_H__
#define __MINIMAL_H__
#include <Eigen/Core>
// base-class defining and interface + common functions
// child-classes with possible overloads. does not work to
// define basic eigen-types as template specialization parameter
template<typename T>
class MinimalBase
{
public:
MinimalBase(const T& val)
:val(val){};
T val;
// pure virtual interface
virtual void setZero() = 0;
// the common functionality
void increase() { val = val*6; };
void decrease() { val = val/7; };
};
// again pure virtual, so that the compiler will not instantiate this when
// trying to deduce the correct template specialization
template<typename T>
class Minimal : public MinimalBase<T>
{
public:
Minimal(const T& val)
:MinimalBase<T>(val){};
virtual void setZero() = 0;
// the rest of the common functionality is inherited from MinimalBase
};
// one spezialization for "int"
template<>
class Minimal<int> : public MinimalBase<int>
{
public:
Minimal()
:MinimalBase<int>(4){};
virtual void setZero() { val = 0; };
};
// this one is actually shorter...
template<>
class Minimal<short> : public MinimalBase<short>
{
public:
Minimal()
:MinimalBase<short>(1){};
virtual void setZero() { val = 0; };
void increase() { val = val*3; };
};
// and the eigen-one (at best limited to vector-like matrices)... how to do this?
template<class Derived>
class Minimal<Eigen::MatrixBase<Derived> > : public MinimalBase<Eigen::MatrixBase<Derived> >
{
public:
Minimal<Eigen::MatrixBase<Derived> >()
:MinimalBase<Eigen::MatrixBase<Derived> >(Eigen::MatrixBase<Derived>::Zero()){};
virtual void setZero() { this->val.setZero(); };
};
#endif
带有 Eigen 内容的最后一个块无法编译。我对如何解决这个问题的总体方向更加迷茫,具体的语法对我来说不是很清楚。使用此标头,以下行将不会在您的通用 minimum-example-main-cpp 中编译(缺少 Eigen-stuff):
Minimal<int>A;
Minimal<short>B;
// this does not work:
Minimal<Eigen::Vector2f>C;
std::cerr << "before: " << A.val << " " << B.val << "\n";
A.increase();
A.decrease();
B.increase();
B.setZero()
std::cerr << "after: " << A.val << " " << B.val << "\n";
编译器错误消息如下所示:
/home/joe/test/test.cpp: In function ‘int main()’:
/home/joe/test/test.cpp:36:29: error: no matching function for call to ‘Minimal<Eigen::Matrix<float, 2, 1> >::Minimal()’
Minimal<Eigen::Vector2f>C;
^
/home/joe/test/test.cpp:36:29: note: candidates are:
In file included from /home/joe/test/test.cpp:7:0:
/home/joe/test/minimal.h:26:9: note: Minimal<T>::Minimal(const T&) [with T = Eigen::Matrix<float, 2, 1>]
Minimal(const T& val)
^
/home/joe/test/minimal.h:26:9: note: candidate expects 1 argument, 0 provided
/home/joe/test/minimal.h:23:7: note: Minimal<Eigen::Matrix<float, 2, 1> >::Minimal(const Minimal<Eigen::Matrix<float, 2, 1> >&)
class Minimal : public MinimalBase<T>
^
/home/joe/test/minimal.h:23:7: note: candidate expects 1 argument, 0 provided
/home/joe/test/test.cpp:36:29: error: cannot declare variable ‘C’ to be of abstract type ‘Minimal<Eigen::Matrix<float, 2, 1> >’
Minimal<Eigen::Vector2f>C;
^
In file included from /home/joe/test/test.cpp:7:0:
/home/joe/test/minimal.h:23:7: note: because the following virtual functions are pure within ‘Minimal<Eigen::Matrix<float, 2, 1> >’:
class Minimal : public MinimalBase<T>
^
/home/joe/test/minimal.h:29:22: note: void Minimal<T>::setZero() [with T = Eigen::Matrix<float, 2, 1>]
virtual void setZero() = 0;
^
编辑:生成的最小示例演示最终找到了github
【问题讨论】:
-
纯虚函数对模板类型推导没有影响。
-
你得到什么编译器错误?
标签: c++ templates inheritance eigen