运算符重载,编译没错,为什么结果不对呢?多谢帮忙看看。
#include"iostream.h"class Matrix
{
int matrix[3][3];
public:
Matrix();
friend Matrix operator+(Matrix &a,Matrix &b);
friend ostream & operator<<(ostream &os,Matrix &m);
friend istream & operator>>(istream &is,Matrix &m);
};
Matrix::Matrix()
{
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
matrix[i][j]=0;
}
Matrix operator+(Matrix &a,Matrix &b)
{
Matrix c;
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
c.matrix[i][j]=a.matrix[i][j]+b.matrix[i][j];
return c;
}
ostream & operator <<(ostream &os,Matrix &m)
{
for(int i=0;i<3;i++)
{
for(int j=0;i<3;j++)
os<<m.matrix[i][j]<<" ";
os<<endl;
}
return os;
}
istream & operator >>(istream &is,Matrix &m)
{
cout<<"请输入矩阵数据:"<<endl;
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
is>>m.matrix[i][j];
return is;
}
void main()
{
Matrix m,m1,m2;
cin>>m1;
cin>>m2;
cout<<"Matrix m1:\n"<<m1<<endl;
cout<<"Matrix m2:\n"<<m2<<endl;
m=m1+m2;
cout<<"Matrix m1+m2=\n"<<m<<endl;
}