简体   繁体   English

在C ++中使用Eigen库时出错

[英]Error in using Eigen library in C++

I am trying to use eigen library in c++, but getting error runtime. 我试图在C ++中使用本征库,但运行时出错。

#include<iostream>
#include <Eigen/Dense>
using namespace Eigen;
int main ()
{
    MatrixXd mat1;
    int i,j;
    i=4;
    j=3;
    mat1(i,j);
    float ct=1.0;


    for(int m=0;i<i;m++)
    {
        for (int n=0;n<j;n++)
        {
            mat1(m,n)= ct;
            ct = ct+1.0;
        }
    }

    std::cout<<"size of matrix is :"<<mat1.size()<<std::endl;
    std::cout<<"rows = "<<mat1.rows()<<"\t columns = "<<mat1.cols();
    //std::cout<<mat1;

}

I have Eigen library extracted at /usr/local/include/ and compiling as g++ test4.cpp -o test4 . 我在/usr/local/include/提取了Eigen库,并编译为g++ test4.cpp -o test4 There is no error at compile time. 编译时没有错误。 But at runtime, I get 但是在运行时,我得到了

test4: /usr/local/include/Eigen/src/Core/DenseCoeffsBase.h:365: Eigen::DenseCoeffsBase<Derived, 1>::Scalar& Eigen::DenseCoeffsBase<Derived, 1>::operator()(Eigen::Index, Eigen::Index) [with Derived = Eigen::Matrix<double, -1, -1>; Eigen::DenseCoeffsBase<Derived, 1>::Scalar = double; Eigen::Index = long int]: Assertion `row >= 0 && row < rows() && col >= 0 && col < cols()' failed.
Aborted (core dumped)

Any suggestions? 有什么建议么? Can someone also suggest how to use my typedef instead of predefined ones? 有人还能建议如何使用我的typedef而不是预定义的吗? Also, is there any special way to display matrix (as written in last line of code)? 另外,是否有任何特殊的方式显示矩阵(如代码的最后一行所示)?

mat1(i,j);

does not do what you think it does. 不按照您的想法去做。 It's the overloaded operator() and merely tries to access an element and returns a reference, it does not resize the matrix. 它是重载的operator() ,仅尝试访问元素并返回引用,它不会调整矩阵的大小。

You need to either do 你要么要做

MatrixXd mat1(i,j);

or use 或使用

mat1.resize(i,j);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM