使用运算符重载添加两个矩阵时显示垃圾值

Showing garbage values when adding two matrix using operator overloading

请帮助我,因为我是编程新手,我已经编写了这个程序,它看起来不错,但我得到的是垃圾值,而不是两个的总和 matrix.I 已将运算符重载的概念应用到找到两个矩阵的总和,但我一次又一次地得到垃圾值?请帮我看看问题出在哪里?Thanks.Any非常感谢帮助

#include<iostream>
#include<string>

using namespace std;
class Matrix {
private:
    int matrix[2][2];
public:
    Matrix operator + (Matrix Sum)
    {
        Matrix sum[2][2];
        for (int i = 0; i < 2; i++)
        {
            for (int j = 0; j < 2; j++)
            {
                sum[i][j].matrix[i][j] = matrix[i][j] + Sum.matrix[i][j];
                return(sum[i][j]);
            }
        }
    }

    void setMatrix(int m[][2])
    {
        for (int i = 0; i < 2; i++)
        {
            for (int j = 0; j < 2; j++)
            {
                matrix[i][j] = m[i][j];
            }
        }
    }
    void Display() 
    {
          cout << "\n\nThe matrix finally equals: ";   
         for (int i = 0; i < 2; i++)
        {
            cout << " ";
            for (int j = 0; j < 2; j++)
            {
               cout<<matrix[i][j];
                if (j == 2 - 1)
                    cout << endl;
            }
        } 
    }
};


int main()
{
    Matrix m1, m2,sum;
   
    const int size=2;
    int matrix1[size][size];
    int matrix2[size][size];
    cout << "Enter the values of matrix 1 (2 X 2)\n\n";
    for (int i = 0; i < size; i++)
    {
        for (int j = 0; j < size; j++)
        {
            cin >> matrix1[i][j];
        }
    }

    cout << "Enter the values of matrix 2 (2 X 2)\n\n";
    for (int i = 0; i < size; i++)
    {
        for (int j = 0; j < size; j++)
        {
            cin >> matrix2[i][j];
        }
    }
    cout <<"\n\nSetting the values now\n\n";
    m1.setMatrix(matrix1);
    m2.setMatrix(matrix2);
    sum = m1 + m2;
    cout << "\n\nMatrix 1 (2 X 2) is : ";
    for (int i = 0; i < size; ++i)
    {
        for (int j = 0; j < size; ++j)
        {
            cout << matrix1[i][j] << "  ";
            if (j == size - 1)
                cout << endl;
        }
    }

    cout << "\n\nMatrix 2 (2 X 2) is : ";
    for (int i = 0; i < size; ++i)
    {
        for (int j = 0; j < size; ++j)
        {
            cout << matrix2[i][j] << "  ";
            if (j == size - 1)
                cout << endl;
        }
    }

    cout << "\n\nSum of two matrices is equal to (2 X 2) is : ";
    sum.Display();
    return 0;
}

我们仔细看看你operator+,主要有两个错误:

  • Matrix sum[2][2];是一个矩阵数组,但是你想return只有一个Matrix,而不是多个。名称也不好,因为您已经有一个名称相似的参数。是的,C++ 是区分大小写的,但是这样相似的名称对于人类读者来说是有问题的 ;)。

  • 看看 return 在哪里。它将 return 第一次迭代期间,例如

    sum[i][j].matrix[i][j] = matrix[i][j] + Sum.matrix[i][j];
    

    将只被调用一次,ij 为零。因此它仅设置 one 条目和 returns 紧随其后(留下其他 3 个值未初始化)。这就是垃圾值的来源,从技术上讲,它是未定义的行为。

函数应该是这样的,但请不要只是复制粘贴,而是花一些时间来理解它。

Matrix operator + (Matrix rhs) // One of the two names needed to change.
{
    Matrix sum; //only one value with a proper name
    for (int i = 0; i < 2; i++)
    {
        for (int j = 0; j < 2; j++)
        {
            // better be explicit and use this, imo
            sum.matrix[i][j] = this->matrix[i][j] + rhs.matrix[i][j];
        }
    }

    return sum; // return only after all the calculations.
}