注册 登录
编程论坛 C++教室

距阵相加时的错误,关于this指针

轮廓 发布于 2007-07-15 19:31, 632 次点击

/*题目:设计一个矩阵类Matrix,完成矩阵的基本运算。
要求:
设计要求如下:
通过重载+,-,*运算符,完成对矩阵加法,减法,合乘法运算。
设计并实现Matrix类
画出Matrix类的UML图。
Matrix类的声明如下:*/

//为什么在void input();//矩阵输入
//void display();//矩阵输出 就是错误呢
//还有在相加 Matrix operator+(Matrix &AMD); 时也错误

谁帮我看一下,谢谢啦


#include<iostream.h>
include<stdlib.h>
//using namespace std;

class Matrix //矩阵类
{

private:
int rows,cols; //矩阵的行列
double *m; //矩阵存放的元素,按行存放

public:
Matrix(int N_rows=2,int M_cols=2);
~Matrix();

MatriX(const Matrix &ZM);

Matrix operator+(Matrix &AMD); //重载+
//Matrix operator-(Matrix &); //重载-
//Matrix operator*(Matrix &); //重载*
void input();//矩阵输入
void display();//矩阵输出
};


Matrix::Matrix(int N_rows,int M_cols)
{
rows=N_rows;
cols=M_cols;

m = new double [rows * cols];


}

Matrix::~Matrix()
{
delete []m;
}

Matrix::MatriX(const Matrix &ZM)
{
rows=ZM.rows;
cols=ZM.cols;

m=new double [rows*cols];

for(int Row=1;Row<=rows;Row++)
for(int Col=1;Col<cols;Col++)
(*this)(Row,Col)=ZM(Row,Col);

}


Matrix Matrix::operator+(Matrix &AMD)
{

if(rows==AMD.rows && cols==AMD.cols)
{
Matrix GM(rows,cols);

for(int Row=1;Row<=rows;Row++)
for(int Col=1;Col<cols;Col++)
GM(Row,Col)=(*this)(Row,Col)+AMD(Row,Col);

return GM;
}

else
{
cout<<"距阵相加运算错误"<<endl;
exit(0);



}

}

void Matrix::input()
{
for(int Row=1;Row<=rows;Row++)
for(int Col=1;Col<cols;Col++)
cin>>*(m++);

}


void Matrix::display()
{
for(int Row=1;Row<=rows;Row++)
{
for(int Col=1;Col<cols;Col++)
cout<<*(m++)<<'\t';

cout<<endl;
}

}

//4.测试代码
int main(void)
{

Matrix a(2,2); //矩阵a 2行2列
Matrix b(2,2); //矩阵b 2行2列
Matrix c(2,2); //矩阵c 2行2列


cout<<"请输入你想要的2行2列的距阵:\t\n";
a.input();


cout<<"请输入你想要的2行2列的距阵:\t\n";
b.input();
cout<<endl;

c=a+b;
cout<<"下面是你想得到的相加后的2行2列的距阵:\t\n\n";
c.display();

//c=a-b;
//cout<<"下面是你想得到的相减后的2行2列的距阵:\t\n\n";
//c.display();

//c=a*b;
//cout<<"下面是你想得到的相乘后的2行2列的距阵:\t\n\n";
//c.display();

return 0;

}

1 回复
#2
轮廓2007-07-17 19:27
没人帮我吗?555555555555555555555555
1