使用EIGEN定义动态矩阵

时间:2014-11-18 02:48:38

标签: c++ visual-studio-2012 matrix eigen

我对此代码有疑问,主要想法是创建一个typedef' Base'使用模板中定义的cols数量:

// --- 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:&#39; _Cols&#39; :&E; :: Matrix&#39;的无效模板参数;预期的编译时常量表达式z:\ desktop \ photoconsistency-visual-odometry-master \ phovo \ include \ Matrix.h 97 1 PhotoconsistencyVisualOdometry

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

1 个答案:

答案 0 :(得分:1)

我可以毫无问题地编译和运行它(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;     
}

作为旁注,您确定要从Eigen派生吗? 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;