当运算符与操作数不匹配时,如何通过重载 * 运算符来乘以矩阵?
How can I multiply matrices by overloading the * operator, when the operator doesn't match the operands?
我有两个矩阵,它们应该通过在构造函数 class 中重载 * 运算符相乘,但这里的问题是没有 operator []
匹配这些操作数。为什么?
我看过视频并多次询问我的 class 队友并尝试了我自己的方法,但我无法做到。我只得到这个错误!
这是我遇到问题的代码:
构造函数代码:
我做了两种方法来使这段代码工作。结果应存储在单元格矩阵或新矩阵中:
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;
}
Header代码:
通常我没有 header 代码,除非需要进行一些修改。
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
假设class矩阵有一个成员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;
}
存在三个问题。首先 class 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;
}
我有两个矩阵,它们应该通过在构造函数 class 中重载 * 运算符相乘,但这里的问题是没有 operator []
匹配这些操作数。为什么?
我看过视频并多次询问我的 class 队友并尝试了我自己的方法,但我无法做到。我只得到这个错误!
这是我遇到问题的代码:
构造函数代码:
我做了两种方法来使这段代码工作。结果应存储在单元格矩阵或新矩阵中:
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;
}
Header代码:
通常我没有 header 代码,除非需要进行一些修改。
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
假设class矩阵有一个成员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;
}
存在三个问题。首先 class 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;
}