哪位能帮我解释一下这个代码哪里出错了!
//重载+,<<,>>运算符,计算二维数组相加。
#include<iostream>
using namespace std;
class Mat
{
public:
friend Mat operator+(Mat& a, Mat& b);
friend istream operator>>(istream& in, Mat& a);
friend ostream operator<<(ostream& cout, Mat& a);
Mat();
private:
int mat[2][3];
};
Mat::Mat()
{
for (int i = 0; i < 2; i++)
for (int j = 0; j < 3; j++)
mat[i][j] = 0;
}
istream operator>>(istream& in, Mat& m)
{
for (int i = 0; i < 2; i++)
for (int j = 0; j < 3; j++)
{
cin >> m.mat[i][j];
};
return cin;
}
ostream operator<<(ostream& cout, Mat& m)
{
for (int i = 0; i < 2; i++)
for (int j = 0; j < 3; j++)
{
cout << m.mat[i][j];
};
return cout;
}
Mat operator+(Mat& a, Mat& b)
{
Mat n;
for (int i = 0; i < 2; i++)
for (int j = 0; j < 3; j++)
{
n.mat[i][j] = a.mat[i][j] + b.mat[i][j];
}
return n;
}
int main()
{
Mat a, b, c;
cin >> a;
cin >> b;
cout << "a=" << a;
cout << "b=" << b;
c = a + b;
cout << "c=" << c;
system("pause");
return 0;
}
在重载输入输出运算符时,应该使用流对象的引用,而不是对象本身。因此,在重载输入和输出运算符时,应该将参数的类型改为istream&
和ostream&
,并在返回值处返回相应的流对象。
此外,在输出运算符中,应该以空格或换行符分隔每个数字,而不是将它们挤在一起输出。
下面是修改后的代码:
#include<iostream>
using namespace std;
class Mat
{
public:
friend Mat operator+(Mat& a, Mat& b);
friend istream& operator>>(istream& in, Mat& a);
friend ostream& operator<<(ostream& cout, Mat& a);
Mat();
private:
int mat[2][3];
};
Mat::Mat()
{
for (int i = 0; i < 2; i++)
for (int j = 0; j < 3; j++)
mat[i][j] = 0;
}
istream& operator>>(istream& in, Mat& m)
{
for (int i = 0; i < 2; i++)
for (int j = 0; j < 3; j++)
{
in >> m.mat[i][j];
};
return in;
}
ostream& operator<<(ostream& cout, Mat& m)
{
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
cout << m.mat[i][j] << " ";
}
cout << endl;
}
return cout;
}
Mat operator+(Mat& a, Mat& b)
{
Mat n;
for (int i = 0; i < 2; i++)
for (int j = 0; j < 3; j++)
{
n.mat[i][j] = a.mat[i][j] + b.mat[i][j];
}
return n;
}
int main()
{
Mat a, b, c;
cin >> a >> b;
cout << "a=" << endl << a;
cout << "b=" << endl << b;
c = a + b;
cout << "c=" << endl << c;
system("pause");
return 0;
}
修改如下:
//重载+,<<,>>运算符,计算二维数组相加。
#include<iostream>
using namespace std;
class Mat
{
public:
friend Mat operator+(Mat& a, Mat& b);
friend istream& operator>>(istream& in, Mat& a); // 返回加个 &
friend ostream& operator<<(ostream& cout, Mat& a); // 返回加个 &
Mat();
private:
int mat[2][3];
};
Mat::Mat()
{
for (int i = 0; i < 2; i++)
for (int j = 0; j < 3; j++)
mat[i][j] = 0;
}
istream& operator>>(istream& in, Mat& m) // 返回加个 &
{
for (int i = 0; i < 2; i++)
for (int j = 0; j < 3; j++)
{
cin >> m.mat[i][j];
};
return cin;
}
ostream& operator<<(ostream& cout, Mat& m) // 返回加个 &
{
for (int i = 0; i < 2; i++)
for (int j = 0; j < 3; j++)
{
cout << m.mat[i][j];
};
return cout;
}
Mat operator+(Mat& a, Mat& b)
{
Mat n;
for (int i = 0; i < 2; i++)
for (int j = 0; j < 3; j++)
{
n.mat[i][j] = a.mat[i][j] + b.mat[i][j];
}
return n;
}
int main()
{
Mat a, b, c;
cin >> a;
cin >> b;
cout << "a=" << a <<endl;
cout << "b=" << b <<endl;
c = a + b;
cout << "c=" << c <<endl;
system("pause");
return 0;
}
这个代码有几个问题:
1.在类的定义中,重载输入输出运算符时应该使用friend istream& operator>>(istream& in, Mat& a)
和friend ostream& operator<<(ostream& cout, Mat& a)
,并且在函数定义中也应该加上返回值类型。
2.在重载输出运算符时,应该在输出每个元素后加上空格或换行,否则输出的结果会挤在一起。
修改后的代码如下:
#include<iostream>
using namespace std;
class Mat
{
public:
friend Mat operator+(Mat& a, Mat& b);
friend istream& operator>>(istream& in, Mat& a);
friend ostream& operator<<(ostream& cout, Mat& a);
Mat();
private:
int mat[2][3];
};
Mat::Mat()
{
for (int i = 0; i < 2; i++)
for (int j = 0; j < 3; j++)
mat[i][j] = 0;
}
istream& operator>>(istream& in, Mat& m)
{
for (int i = 0; i < 2; i++)
for (int j = 0; j < 3; j++)
{
in >> m.mat[i][j];
};
return in;
}
ostream& operator<<(ostream& cout, Mat& m)
{
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 3; j++)
{
cout << m.mat[i][j] << " ";
}
cout << endl;
}
return cout;
}
Mat operator+(Mat& a, Mat& b)
{
Mat n;
for (int i = 0; i < 2; i++)
for (int j = 0; j < 3; j++)
{
n.mat[i][j] = a.mat[i][j] + b.mat[i][j];
}
return n;
}
int main()
{
Mat a, b, c;
cin >> a;
cin >> b;
cout << "a=" << endl << a;
cout << "b=" << endl << b;
c = a + b;
cout << "c=" << endl << c;
system("pause");
return 0;
}
在istream和ostream的函数声明中,返回类型应为istream&和ostream&,而不是istream和ostream:
istream& operator>>(istream& in, Mat& m)
{
for (int i = 0; i < 2; i++)
for (int j = 0; j < 3; j++)
{
cin >> m.mat[i][j];
};
return cin;
}
ostream& operator<<(ostream& cout, Mat& m)
{
for (int i = 0; i < 2; i++)
for (int j = 0; j < 3; j++)
{
cout << m.mat[i][j];
};
return cout;
}
// 表删除
Status ListDelete_Sq(SqList& L, int i,ElemType &e) {
if ((i < 1) || (i > L.length))return ERROR; //判断i值不合法
e = L.elem[i - 1];
for (int j = i; j <= L.length - 1; j++)
L.elem[j - 1] = L.elem[j]; //被删除元素之后的元素向前迁移
L.length--; //表长减1
return OK;
}