我正在编写一个简单的程序来定义具有表示状态的向量的系统。我想根据导出类中状态的数量来确定特征向量的声明类型。
我试图使用别名模板来实现这一点,类似于下面的代码
#include <iostream>
#include <Eigen/Core>
#include <Eigen/Dense>
using namespace std;
using namespace Eigen;
class A
{
public:
template <int T>
using StateVector = typename Matrix<double, T, 1>;
};
class B : public A
{
public:
int NUM_STATES = 5;
B(){
StateVector<NUM_STATES> a;
a.setIdentity();
cout<<a<<endl;
}
};
int main(){
B b;
}
我最终希望拥有一个可以在派生类中使用的类型。这可能吗?
答案 0 :(得分:1)
进行两个小改动,您的代码就可以正常工作。
首先,这里不能有typename
关键字:
template <int T>
using StateVector = Matrix<double, T, 1>;
第二,NUM_STATES
必须是编译时常量,即,可以将其声明为enum
的元素,也可以声明为static const int
(或static constexpr int
,如果愿意的话) ):
static const int NUM_STATES = 5;
关于Godbolt的完整示例:https://godbolt.org/z/_T0gix