自己编了一个关于矩阵的程序.windows编译时没有错误.但是说程序运行出错.请指教,谢谢~
#include <iostream>
using namespace std;
class matrix
{
public:
matrix (int m=2,int n=2);
matrix (matrix &m);
void input();
void output();
matrix operator + (matrix &m);
matrix operator - (matrix &m);
matrix operator * (matrix &m);
matrix & operator = (matrix &m);
~matrix();
private:
int str,col,**matr;
};
matrix::matrix (int m,int n)
{
str=m;
col=n;
matr=new int *[str];
for (int i=0; i<str; ++i)
matr[i]= new int[col];
for (i=0; i<str; ++i)
for (int j=0; j<col; j++)
matr[i][j]=0;
};
void matrix::input()
{
cout<< "input matrix"<< endl;
for (int i=0; i<str; ++i)
for (int j=0; j<col; ++j)
cin>> matr[i][j];
}
void matrix::output()
{
for (int i=0; i<str; ++i)
{
for (int j=0; j<col; ++j)
cout<< matr[i][j]<<' ';
cout<<endl;
}
}
matrix::matrix (matrix &m)
{
for (int i=0; i<str; ++i)
for (int j=0; j<col; ++j)
matr[i][j]=m.matr[i][j];
}
matrix matrix::operator + (matrix &m)
{
matrix *n=new matrix(str,col);
if (str != m.str || col != m.col)
{
cout<<" + error"<<endl;
return *n;
}
for (int i=0; i<str; ++i)
for (int j=0; j<col; ++j)
n->matr[i][j]=matr[i][j]+m.matr[i][j];
return *n;
}
matrix matrix::operator - (matrix &m)
{
matrix *n=new matrix(str,col);
if (str != m.str || col != m.col)
{
cout<<" - error"<<endl;
return *this;
}
for (int i=0; i<str; ++i)
for (int j=0; j<col; ++j)
n->matr[i][j]=matr[i][j]-m.matr[i][j];
return *n;
}
matrix matrix::operator * (matrix &m)
{
matrix *n=new matrix(str,col);
if (col != m.str)
{
cout<<"* error"<<endl;
return *this;
}
for (int i=0; i<str; ++i)
for (int j=0; j<col; ++j)
for (int t=0; t<col; ++t)
n->matr[i][j]+=matr[i][t]*m.matr[t][i];
return *n;
}
matrix matrix::operator = (matrix &m)
{
if (str != m.str || col != m.col)
{
cout<<"= error"<<endl;
cout<< col <<" "<< m.col<<endl;
return *this;
}
for (int i=0; i<str; ++i)
for (int j=0; j<col; ++j)
matr[i][j]=m.matr[i][j];
return *this;
}
matrix::~matrix()
{
for (int i=0; i<str; ++i)
delete [] matr[i];
delete [] matr;
}
int main()
{
matrix m1,m2,m3;
m1.input();
m2.input();
m3=m1+m2;
m3.output();
m3=m1-m2;
m3.output();
m3=m1*m2;
m3.output();
return 0;
}