简体   繁体   中英

Showing garbage values when adding two matrix using operator overloading

Please help me as I am new to programming and I have made this program and it looks fine to me but I am getting garbage values instead of sum of two matrix.I have applied the concept of operator overloading to find the sum of two matrices but I am getting garbage values again and again?Please help me that where is the problem?Thanks.Any help is completely appreciated

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

在此处输入图像描述

Let's take a close look at you operator+ , there are two major errors:

  • Matrix sum[2][2]; is an array of matrices, but you want to return only a single Matrix , not multiple. Also the name is bad, because you already have a parameter with a similiar name. And yes, C++ is case sensitive, but such similiar names are problematic for human readers;).

  • Look where the return is. It will return during the first iteration, eg

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

    will be called just once, with i and j being zero. Thus it sets only one entry and returns immediately afterwards (leaving the other 3 values uninitialized). That's where the garbage values come from, technically it's undefined behaviour.

Here is what the function should look like, but please don't just copy-paste, but take some time to understand it.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM