当运算符与操作数不匹配时,如何通过重载*运算符来乘法矩阵?

时间:2019-05-01 14:32:22

标签: c++ matrix operator-overloading matrix-multiplication

我有两个矩阵,应该通过在构造函数类中重载*运算符来相乘,但是这里的问题是没有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  

2 个答案:

答案 0 :(得分:0)

假设类矩阵具有成员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的一列,而应将一行乘以一列。

答案 1 :(得分:-1)

在这里得到我的答案:

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;
}