[英]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:让我们仔细看看你
operator+
,有两个主要错误:
Matrix sum[2][2];
is an array of matrices, but you want to return only a single Matrix
, not multiple.是一个矩阵数组,但您只想返回一个
Matrix
,而不是多个。 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;).
是的,C++ 是区分大小写的,但是这样的相似名称对于人类读者来说是有问题的;)。
Look where the return
is.看看
return
在哪里。 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.将只被调用一次,其中
i
和j
为零。 Thus it sets only one entry and returns immediately afterwards (leaving the other 3 values uninitialized).因此它只设置一个条目并在之后立即返回(其他 3 个值未初始化)。 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.这是 function 应该是什么样子,但请不要只是复制粘贴,而是需要一些时间来理解它。
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.
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.