我有两个矩阵,它们应该在构造函数类中重载*运算符,但这里的问题是没有operator []
匹配这些操作数。为什么?
我看了几个视频,并多次问我的同学,并尝试了自己的方式,但我不能让它工作。我只收到这个错误!
这是我遇到问题的代码:
我提出了两种方法来使这段代码有效。结果应存储在单元矩阵或新矩阵中:
Matrix operator*(const Matrix &matrix1, const Matrix &matrix2)
{
if (matrix1.Cols != matrix2.Rows) {
throw("Error");
}
cell.resize(matrix2.Cols); // one way to call
Matrix res(matrix1.Rows, matrix2.Cols, 1.0); // second way to call
for (int i = 0; i < matrix1.Rows; i++) {
cell[i].resize(matrix1.Rows);
for (int j = 0; j < matrix2.Cols; j++) {
double value_of_elements;
for (int k = 0; k = matrix1.Cols; k++) {
res[i][j] += matrix1[i][k] * matrix2[i][j];//
1. metod
value_of_elements += matrix1[i][k] *
matrix2[i][j];// 2. metod
}
cell[i][j]+=value_of_elements;
}
}
return res;
}
除非进行一些修改,否则通常我没有标题代码。
friend Matrix operator*(const Matrix &matrix1, const Matrix &matrix2);
这是代码测试的地方:
try {
Matrix m1(3, 3, 1.0);
Matrix m2(3, 4, 1.0);
std::cout << "m1*m2:" << m1 * m2 << std::endl;// this si where the matrix should be multiplied here;
}
catch (std::exception &e) {
std::cout << "Exception: " << e.what() << "!" << std::endl;
}
catch (...) {
std::cout << "Unknown exception caught!" << std::endl;
}
system("pause");
return 0;
}
结果应该是这样的:
m1*m2:[3, 3, 3, 3
3, 3, 3, 3
3, 3, 3, 3]
我得到的是一个错误;错误的原因是res[i][j]
,matrix1[i][k]
等运算符[]不适用于这些操作数:
Error C2065 'cell': undeclared identifier 71 matrix.cpp
Error C2065 'cell': undeclared identifier 74 matrix.cpp
Error C2065 'cell': undeclared identifier 81 matrix.cpp
Error C2088 '[': illegal for class 79 matrix.cpp
Error C2088 '[': illegal for class 78 matrix.cpp
Error C2676 binary '[': 'Matrix' does not define this operator or a conversion to a type acceptable to the predefined operator 78 matrix.cpp
Error C2676 binary '[': 'const Matrix' does not define this operator or a conversion to a type acceptable to the predefined operator 78 matrix.cpp
Error C2676 binary '[': 'const Matrix' does not define this operator or a conversion to a type acceptable to the predefined operator 79 matrix.cpp
Error (active) E0020 identifier "cell" is undefined 71 Matrix.cpp
Error (active) E0349 no operator "[]" matches these operands 78 Matrix.cpp
Error (active) E0349 no operator "[]" matches these operands 78 Matrix.cpp
Error (active) E0349 no operator "[]" matches these operands 78 Matrix.cpp
Error (active) E0349 no operator "[]" matches these operands 79 Matrix.cpp
Error (active) E0349 no operator "[]" matches these operands 79 Matrix.cpp
假设类矩阵有成员vector<vector<double>> cell
,这里是一个乘以矩阵的样本:
Matrix operator*(const Matrix &matrix1, const Matrix &matrix2)
{
if (matrix1.Cols != matrix2.Rows) {
throw("Error");
}
Matrix res(matrix1.Rows, matrix2.Cols, 1.0);
for (int i = 0; i < matrix1.Rows; i++) {
for (int j = 0; j < matrix2.Cols; j++) {
double value_of_elements=0;
for (int k = 0; k = matrix1.Cols; k++)
value_of_elements += matrix1.cell[i][k] * matrix2.cell[k][j];
res.cell[i][j]=value_of_elements;
}
}
return res;
}
有三个问题。首先,类Matrix
没有运算符[]。通过直接访问成员cell
解决了这个问题。其次,变量value_of_elements
未初始化,结果未定义。第三,矩阵乘法没有正确完成。您将matrix1
中的一列与matrix2
中的一列相乘,而您应该将一列乘以一列。
得到了我的回答:
Matrix operator*(const Matrix &matrix1, const Matrix &matrix2)
{
if (matrix1.Cols != matrix2.Rows) {
throw("Error");
}
Matrix res(matrix1.Rows, matrix2.Cols, 0.0);
for (int i = 0; i < matrix1.Rows; i++) {
for (int j = 0; j < matrix2.Cols; j++) {
double value_of_elements;
for (int k = 0; k < matrix1.Cols; k++) {
res.cell[i][j] += matrix1.cell[i][k] * matrix2.cell[i][j];
}
}
}
return res;
}