【问题标题】:How can I multiply matrices by overloading the * operator, when the operator doesn't match the operands?当运算符与操作数不匹配时,如何通过重载 * 运算符来将矩阵相乘?
【发布时间】:2019-05-01 14:32:21
【问题描述】:

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

【问题讨论】:

  • 显然,您的Matrix 类不提供operator[]。您觉得错误消息的哪一部分不清楚?
  • Matrix 是否有包含这些值的成员?例如,如果您有成员 vector&lt;vector&lt;double&gt;&gt; cell,则可以使用 res.cell[i][j] 访问它。如果你写res[i][j],编译器期望找到一个在类Matrix本身中声明的运算符[]。
  • 好的,我试试这个...
  • 另外,该函数不是类的成员。如果cell 是对象res 的一部分,则始终需要编写res.cell
  • 好吧知道它有效,但这是第二个问题,矩阵超出范围......

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


【解决方案1】:

假设类矩阵有一个成员vector&lt;vector&lt;double&gt;&gt; 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 的一列相乘,而您应该将一行乘以一列。

【讨论】:

  • 当你解决你的问题时,请把它作为答案。它告诉人们,当他们不需要时,他们不必再次解决问题。
  • 我把它放在哪里?
【解决方案2】:

在这里得到我的答案:

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

【讨论】:

  • 索引中有错误,如我的回答中所述。而不是“m1[i][k]*m2[i][j]”,应该是“m1[i][k]*m2[k][j]”。
  • 这个答案没有解释问题是什么,所以下一个有这个问题的人很难使用这个答案。请添加一些文字来解释更改的内容和原因。
  • @ZainAhmed StackOverflow 问题来自个人问题,但也作为问答数据库帮助其他人解决类似问题。这就是为什么在您自己找到解决方案时回答您自己的问题很重要,而且回答时要提供所有必要的详细信息,以便其他人了解您是如何解决的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-23
  • 2015-07-29
  • 2012-03-09
  • 1970-01-01
  • 2012-02-26
相关资源
最近更新 更多