我使用特征库和它的块操作,根据https://eigen.tuxfamily.org/dox/group__TutorialBlockOperations.html这些块都是左值和右值。但下面的程序不能编译:特征块不是左值?
#include "Eigen/Dense"
#include <iostream>
int main() {
Eigen::Matrix<float, Eigen::Dynamic, Eigen::Dynamic> m(4, 4);
m << 1, 2, 3, 4,
5, 6, 7, 8,
9, 10, 11, 12,
13, 14, 15, 16;
Eigen::Matrix<float, Eigen::Dynamic, Eigen::Dynamic> &column = m.col(0);
}
在这个程序中,我想使用列作为参考矩阵m,所以我可以在原地更改元素。错误信息是:
test_eigen_rvalue.cc: In function ‘int main()’:
test_eigen_rvalue.cc:11:73: error: invalid initialization of non-const reference of type ‘Eigen::Matrix<float, -1, -1>&’ from an rvalue of type ‘Eigen::DenseBase<Eigen::Matrix<float, -1, -1> >::ColXpr {aka Eigen::Block<Eigen::Matrix<float, -1, -1>, -1, 1, true>}’
Eigen::Matrix<float, Eigen::Dynamic, Eigen::Dynamic> &column = m.col(0);
看来问题是m.col(0)不是左值。
您是否检查col的返回值?你可能需要做的就是改变对const引用的引用。 –
链接的页面错误地使用了“右值”和“左值”。 'm.col(0)'是一个右值,而不是左值。该页面试图说你可以写'm.col(0)= whatever;'来写入列。 –