| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 626 人关注过本帖
标题:距阵相加时的错误,关于this指针
只看楼主 加入收藏
轮廓
Rank: 1
等 级:新手上路
帖 子:21
专家分:0
注 册:2007-4-20
收藏
 问题点数:0 回复次数:1 
距阵相加时的错误,关于this指针

/*题目:设计一个矩阵类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;

}

搜索更多相关主题的帖子: Matrix 指针 AMD 矩阵 相加 
2007-07-15 19:31
轮廓
Rank: 1
等 级:新手上路
帖 子:21
专家分:0
注 册:2007-4-20
收藏
得分:0 
没人帮我吗?555555555555555555555555
2007-07-17 19:27
快速回复:距阵相加时的错误,关于this指针
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.022143 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved