error: invalid types for array subscripts

error: invalid types for array subscripts

我正在尝试在 screen.My 代码上打印矩阵:

#include <vector>
#include <algorithm>
#include <cmath>
#include <eigen2/Eigen/Core>

int main() {
    std::vector<int> items = {1,2,3,4,5,6,7,8,9,10,11,12,18};

    // generate similarity matrix
    unsigned int size = items.size();
    Eigen::MatrixXd m = Eigen::MatrixXd::Zero(size,size);

    for (unsigned int i=0; i < size; i++) {
        for (unsigned int j=0; j < size; j++) {
            // generate similarity
            int d = items[i] - items[j];
            int similarity = exp(-d*d / 100);
            m(i,j) = similarity;
            m(j,i) = similarity;
        }
    }
      for (unsigned int i=0; i < size; i++) {
        for (unsigned int j=0; j < size; j++) {
           std::cout << m[i][j];
        }
     std::cout << std::endl;
    }
    return 0;
}

编译时我得到了这个:

pex.cpp: In function ‘int main()’:
pex.cpp:25:31: error: invalid types ‘Eigen::ei_traits<Eigen::Matrix<double, 10000, 10000> >::Scalar {aka double}[unsigned int]’ for array subscript
            std::cout << m[i][j];

为什么我这里有无效的类型?或者有没有其他方法可以在屏幕上打印内容?我以后的代码会更大,所以我想检查每一步的计算。

没有an operator不用手写循环就可以显示矩阵吗?

std::cout << m << std::endl;

否则,您可能想调用 operator() 而不是 operator[]:

std::cout << m(i, j);

不是吗?