访问Eigen矩阵时C ++断言

时间:2015-01-29 19:18:59

标签: c++ eigen

这是我在Stackoverflow上提出的question之后的问题。我尝试在C ++中使用名称空间和特征库。这就是我编码的内容:

// test.cpp file
#include <iostream>
#include "Eigen/Dense"
#include <cstdlib>
#include "goo.h"
using namespace Eigen;
using namespace std;
using namespace goo;
//    
int main()
{
   void foo();
   foo();
}

// Let foo be a function which operates on an array "myarray" which I want to define in namespace goo
void foo()
{
   int i;
   int j;
   cout << "Enter N:" << endl;
   cin  >> N ;

   for (i=0; i<N; i++)
   {
      for(j=0; j<N; j++)
      {
         myarray(i,j) = i*j;
      }
   }

 }

和goo.h文件是:

using namespace Eigen; 
namespace goo
{
   int N;
   MatrixXi myarray(N,N); // I want to define a N x N myarray matrix here using the eigen library
}

在N = 2运行之后,我收到:

$ foo.exe
Enter N:
2
Assertion failed!

Program: c:\Test\foo.exe
File: C:/eigen/Eigen/src/Core/DenseCoeffsBase.h, Line 337

Expression: row >= 0 && row < rows() && col >= 0 && col < cols()

This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.

我是C ++和Eigen的新手,但我怀疑在我尝试在for循环中放入值之前没有分配矩阵myarray。我如何确保输入的N的值被传递到goo名称空间,以便myarray被正确初始化,我可以在for循环或其他地方使用矩阵。

1 个答案:

答案 0 :(得分:0)

在命名空间goo中,您定义N而不初始化它,然后将该值传递给MatrixXi构造函数。 N与您在N内声明的foo()不同。您需要在相同的范围内声明它们以执行您想要执行的操作。