我从Eigen documentation网站上获得了有关切片和inexing的这段代码:
#include <iostream>
#include <Eigen>
#include <vector>
using namespace std;
using namespace Eigen;
int main() {
std::vector<int> ind{4,2,5,5,3};
MatrixXi A = MatrixXi::Random(4,6);
cout << "Initial matrix A:\n" << A << "\n\n";
cout << "A(all,ind):\n" << A(all,ind) << "\n\n";
return 0;
}
[当我尝试编译时,出现多个错误,例如:
all
不是Eigen
的成员all
未在此范围内声明last
未在此范围内声明seq
不是Eigen
的成员seq
MatrixXi::Random
无效参数我该如何解决这些错误?
[好像我使用的Eigen版本错误(它的工作是here),但是,根据this的答案,我有:EIGEN_WORLD_VERSION 3
EIGEN_MAJOR_VERSION 3
EIGEN_MINOR_VERSION 7
,我相信这是最新的。
就安装而言,我将Eigen
文件夹复制到项目位置,并为g ++编译器提供了该文件夹的路径(-I标志)。图书馆本身似乎运作良好;例如,此代码(来自提供的示例)可以正常工作:
#include <iostream>
#include <Eigen>
//#include <Eigen/Dense> // I had to change this though
// I get an error: 'no such file or directory'
using namespace Eigen;
using namespace std;
int main()
{
Matrix3d m = Matrix3d::Random();
m = (m + Matrix3d::Constant(1.2)) * 50;
cout << "m =" << endl << m << endl;
Vector3d v(1,2,3);
cout << "m * v =" << endl << m * v << endl;
}
您的问题可能是g ++的-I
选项
我怀疑您有类似的东西:
g++ .... -I<path_to_project>/Eigen
...,而应仅是
g++ .... -I<path_to_project>
...即最终Eigen
目录不应位于包含路径上。
进行此更改,然后还更改源代码,以便所有包含的内容都类似于:
#include <Eigen/Dense>
#include <Eigen/Cholesky>
另外,您引用的是变量all
,但您的程序尚未定义它。
还要确保您至少启用了c ++ 11编译器选项。