求大神指导,关于文件流操作符重载的有关问题

求大神指导,关于文件流操作符重载的问题我写了个矩阵相加的程序,结果编译不过去了,是流运算符重载的问题//

求大神指导,关于文件流操作符重载的问题
我写了个矩阵相加的程序,结果编译不过去了,是流运算符重载的问题
//======================
#include<fstream>
#include<cstdlib>
#include<cmath>
using namespace std;
ifstream fin ("Matrix.in");
ofstream fout("Matrix.out");

const int MAX_NUMBER=300;

class Matrix
{
    public:
        int length,width;
        int a[MAX_NUMBER][MAX_NUMBER];
        Matrix():length(3),width(3){memset(a,0x00,sizeof(a));}
        void set_val(int l,int w){length=l;width=w;}
        ifstream& operator >>(ifstream& in)
        {
            for(int i=0;i<width;i++)
            for(int j=0;j<length;j++)
            in>>a[i][j];
            return in;
        }
        ofstream& operator << (ofstream& o)
        {
            for(int i=0;i<width;i++)
            {
                for(int j=0;j<length;j++)
                {
                    o<<a[i][j]<<' ';
                }
                o<<endl;
            }
            return o;
        }
        Matrix operator + (const Matrix& m)
        {
            Matrix sum;
            for(int i=0;i<width;i++)
            for(int j=0;j<length;j++)
                sum.a[i][j]=a[i][j]+m.a[i][j];
            return sum;
        }
        ~Matrix(){fout<<"Destructor called"<<endl;}
}A,B;

int main()
{
    fout<<A+B<<endl;
    return 0;
}
//======================================

49  no match for 'operator<<' in 'fout << Matrix::operator+(const Matrix&)(((const Matrix&)((const Matrix*)(&B))))' 

//======================================
求指导~~~~

类 运算符重载
[解决办法]
ofstream& operator << (ofstream& o)

-->

ofstream& operator << (ofstream& o, const Matrix& mat )

此外,矩阵计算可以使用STL的valarray,valarray使用了表达式模板,性能好得多,你这样写,临时对象的复制会成为热点。
[解决办法]

引用:
friend ofstream& operator << (ofstream& o)

-->

friend ofstream& operator << (ofstream& o, const Matrix& mat )

此外,矩阵计算可以使用STL的valarray,valarray使用了表达式模板,性能好得多,你这样写,临时对象的复制会成为热点。

楼上所述非常正确。建议多用STL,自己写不安全,还费力,而且未必快
[解决办法]

#include<fstream>
#include<cstdlib>
#include<cmath>
using namespace std;
ifstream fin ("Matrix.in");
ofstream fout("Matrix.out");

const int MAX_NUMBER=300;

class Matrix
{
    public:
        int length,width;
        int a[MAX_NUMBER][MAX_NUMBER];
        Matrix():length(3),width(3){memset(a,0x00,sizeof(a));}
        void set_val(int l,int w){length=l;width=w;}
        friend ifstream& operator >>(ifstream& in, Matrix &mat)//改为友元函数。。
        {
            for(int i=0;i<mat.width;i++)
            for(int j=0;j<mat.length;j++)
in>>mat.a[i][j];
            return in;
        }
        friend ofstream& operator << (ofstream& o, Matrix &mat)//改为友元函数
        {
            for(int i=0;i<mat.width;i++)
            {
                for(int j=0;j<mat.length;j++)
                {
                    o<<mat.a[i][j]<<' ';
                }
                o<<endl;
            }


            return o;
        }
        Matrix operator + (const Matrix& m)
        {
            Matrix sum;
            for(int i=0;i<width;i++)
            for(int j=0;j<length;j++)
                sum.a[i][j]=a[i][j]+m.a[i][j];
            return sum;
        }
        ~Matrix(){fout<<"Destructor called"<<endl;}
}A,B;

int main()
{
    fout<<A+B<<endl;
    return 0;
}


[解决办法]
15      Matrix():length(3),width(3){memset(a,0x00,sizeof(a));}
应改为
15      Matrix():length(3),width(3){memset(a,0x00,MAX_NUMBER*MAX_NUMBER*sizeof(int));}