【发布时间】:2021-02-28 20:08:32
【问题描述】:
我有一个复数类,它的数据成员是 im(虚部)和 re(实部)。现在我想对来自类 Matrix(A 和 B)的 2 个矩阵求和,并将总和放入另一个矩阵(C)中。这是我尝试过的。有什么建议吗?
我得到的错误是
no match for 'operator[]' (operand types are 'Matrix' and 'int')
inline Matrix& operator+(Matrix& A, Matrix& B)
{
Matrix *C = new Matrix;
C->rows = A.rows;
C->columns = A.columns;
for(int i = 0; i < A.rows; i++)
for(int j = 0; j < A.columns; j++)
C[i][j] = A[i][j] + B[i][j];
}
return **C;
}
【问题讨论】:
-
为什么要用
new来创建矩阵?使用Matrix C;,并将返回类型更改为Matrix。参数类型也应更改为const Matrix &。如果没有看到确切的错误消息和minimal reproducible example,很难说更多。 -
回答编辑:错误意味着您没有为矩阵类提供重载的
operator[],因此编译器不知道A[i][j](和其他)的含义。跨度>