C++ 编译时出现错误C2676

C++ 编译时出现错误C2676,c++,C++,我尝试用C++编写代码(使用模板)在2个矩阵之间添加。 我在.h文件中有以下代码 #ifndef __MATRIX_H__ #define __MATRIX_H__ //*************************** // matrix //*************************** template <class T, int rows, int cols> class matrix { public: T mat[rows][col

我尝试用C++编写代码(使用模板)在2个矩阵之间添加。 我在.h文件中有以下代码

#ifndef __MATRIX_H__
#define __MATRIX_H__

//***************************
//         matrix
//***************************

template <class T, int rows, int cols> class matrix {
public:
    T mat[rows][cols];
    matrix();
    matrix(T _mat[rows][cols]);
    matrix operator+(const matrix& b);
};

template <class T, int rows, int cols> matrix <T,rows,cols> :: matrix (T _mat[rows][cols]){
    for (int i=0; i<rows; i++){
        for (int j=0; j<cols; j++){
            mat[i][j] = _mat[i][j];
        }
    }
}

template <class T, int rows, int cols> matrix <T,rows,cols> :: matrix (){
    for (int i=0; i<rows; i++){
        for (int j=0; j<cols; j++){
            mat[i][j] = 0;
        }
    }
}

template <class T, int rows, int cols> matrix <T,rows,cols> matrix <T,rows,cols>::operator+(const matrix<T, rows, cols>& b){
    matrix<T, rows, cols> tmp;
    for (int i=0; i<rows; i++){
        for (int j=0; j<cols; j++){
            tmp[i][j] = this->mat[i][j] + b.mat[i][j];
        }
    }
    return tmp;
}



#endif
\ifndef\uu矩阵__
#定义矩阵__
//***************************
//母体
//***************************
模板类矩阵{
公众:
T mat[行][列];
矩阵();
矩阵(T_mat[行][cols]);
矩阵算子+(常数矩阵&b);
};
模板矩阵::矩阵(T_mat[行][cols]){
对于(int i=0;i直线:

tmp[i][j] = this->mat[i][j] + b.mat[i][j]; 
应该是:

tmp.mat[i][j] = this->mat[i][j] + b.mat[i][j]; 

您试图直接为
tmp
变量编制索引,该变量的类型为
matrix
。因此,它抱怨
matrix
类没有提供
操作符[]

的实现,因为
tmp
类型为
matrix
,如下:

tmp[i][j] = ...
使用您尚未定义的
matrix::operator[]
。您可能想说

tmp.mat[i][j] = ...

对于未来,我建议对你的问题使用一个更具描述性的标题,而不仅仅是错误编号。大多数人都不知道VC错误编号,因此无法一眼看出他们是否能够回答你的问题。因为你帮了大忙,我还有一个问题。
tmp.mat[i][j] = ...