我重载了一个operator *()函数来计算两个矩阵的和,但是编译器提示出现此错误

时间:2019-04-10 09:21:04

标签: c++

我使用行数据透视顺序将矩阵映射到一维数组

template<class T>
matrix<T> matrix<T>::operator*(const matrix<T>& m) const {
    if (theCols != m.theRows) throw exception();
    matrix<T> w(theRows, m.theCols);
    for (int i = 1; i <= theRows; i++) {
        for (int j = 1; j < m.theCols; j++) {
            for (int k = 1; k <= theCols; k++) {
                w[(i - 1)*m.theCols + j - 1] += element[(i - 1)*theCols + j - 1] * m.element[(k - 1)*m.theCols + j - 1];
            }
        }
    }
    return w;
}
  

错误:二进制“ [”:“矩阵”未定义此运算符或未转换为预定义运算符可接受的类型

我的代码怎么了?

1 个答案:

答案 0 :(得分:0)

您在这里有错字:

           w[(i - 1)*m.theCols + j - 1] += element[(i - 1)*theCols + j - 1] * m.element[(k - 1)*m.theCols + j - 1];
  //       ^^^^

您可能是说:

            w.element[(i - 1)*m.theCols + j - 1] += element[(i - 1)*theCols + j - 1] * m.element[(k - 1)*m.theCols + j - 1];
//           ^^^^^^^^