| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 696 人关注过本帖
标题:[求助]重载运算符的问题
只看楼主 加入收藏
hahahy
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2006-4-8
收藏
 问题点数:0 回复次数:3 
[求助]重载运算符的问题

自己编了一个关于矩阵的程序.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;
}


搜索更多相关主题的帖子: 算符 载运 
2006-04-15 06:10
johnson1982
Rank: 1
等 级:新手上路
帖 子:27
专家分:0
注 册:2006-3-11
收藏
得分:0 

说明:
首先,在定义一个类时,在类中不能初始化!!
其次,* 是不能重载的!!!
修改代码如下:

#include <iostream>
using namespace std;

class matrix
{
private:
int str,col,**matr;

public:
matrix (int m,int n);
matrix (matrix &m);
void input();
void output();
matrix operator + (matrix &m);
matrix operator - (matrix &m);
//matrix operator * (matrix &m);
matrix & operator = (matrix &m);
~matrix();

};

matrix::matrix (int m,int n)
{
m=2;
n=2;
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::private *(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;
}
*/


2006-04-15 10:08
hahahy
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2006-4-8
收藏
得分:0 

谢谢楼上johnson1982的回复~

已经按这个方法试过了,但是还是有问题.我的C++编译器试microsoft vc++
用debug查停在复制函数处.不知有何错误,望指点!

2006-04-17 00:59
hahahy
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2006-4-8
收藏
得分:0 

小弟刚刚查过书,楼上的大大好像错了...
可以在定义类的时候初始化,*也可以重载~

help

2006-04-17 10:47
快速回复:[求助]重载运算符的问题
数据加载中...
 
   



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

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