使用特征定义动态矩阵

Define a dynamic matrix with EIGEN

本文关键字:动态 定义 特征      更新时间:2023-10-16

我对这段代码有问题,主要思想是使用模板中定义的列数创建一个typedef'Base':

// --- Row dynamic matrix
template< class T, int cols >
class RowDynamicMatrixRowMajor : public Eigen::Matrix< T, Eigen::Dynamic, cols,
      Eigen::RowMajor | Eigen::AutoAlign >{
public:
  typedef Eigen::Matrix< T, Eigen::Dynamic, cols, Eigen::RowMajor | Eigen::AutoAlign > Base;
  RowDynamicMatrixRowMajor( void ) : Base()
  {}
  template< typename OtherDerived >
  RowDynamicMatrixRowMajor( const Eigen::MatrixBase< OtherDerived > & other )
      : Base( other )
  {}
  template< typename OtherDerived >
  RowDynamicMatrixRowMajor & operator= ( const Eigen::MatrixBase< OtherDerived > & other )
  {
    this->Base::operator=( other );
    return *this;
  }
};

但是,在Visual Studio 2012中编译代码时,我收到此错误:

错误

1 错误 C2975:"_Cols":"特征::矩阵"的模板参数无效;预期的编译时常量表达式 z:\desktop\光一致性-视觉里程计-主\phovo\include\Matrix.h 97 1 光一致性视觉测程法

对于该文件,有 20 个类似的错误,但我无法识别错误。

我可以毫无问题地编译和运行它(g ++ 4.9),所以这不是错误的根源。虽然这不是答案,但我发布了,因为我无法在评论中发布代码。

template< class T, int cols >
class RowDynamicMatrixRowMajor : public Eigen::Matrix< T, Eigen::Dynamic, cols, 
      Eigen::RowMajor | Eigen::AutoAlign >
{
public:
  typedef Eigen::Matrix< T, Eigen::Dynamic, cols, Eigen::RowMajor | Eigen::AutoAlign > Base;
  RowDynamicMatrixRowMajor( void ) : Base()
  {}
  template< typename OtherDerived >
  RowDynamicMatrixRowMajor( const Eigen::MatrixBase< OtherDerived > & other )
      : Base( other )
  {}
  template< typename OtherDerived >
  RowDynamicMatrixRowMajor & operator= ( const Eigen::MatrixBase< OtherDerived > & other )
  {
    this->Base::operator=( other );
    return *this;
  }
};
int main()
{
    RowDynamicMatrixRowMajor<double, 10> a;     
}

作为旁注,你确定要从本征派生吗?http://eigen.tuxfamily.org/dox/TopicCustomizingEigen.html

我发现键入我需要的东西要简单得多(您需要对模板别名的 C++11 支持),例如在您的情况下:

template<typename Scalar, int cols> // Eigen::MatrixX<type> (where type = Scalar)
using RowDynamicMatrixRowMajor = 
      Eigen::Matrix<Scalar, Eigen::Dynamic, cols, Eigen::RowMajor | Eigen::AutoAlign>;

然后使用它,例如

RowDynamicMatrixRowMajor<double, 10> a;