有人能找出这段程序的问题吗???可加分@@@

win32c 2002-11-14 01:59:09
#include <iostream.h>
//定义矩阵类
class matrix
{
short rows, cols;//表示矩阵的行、列
double * elems;//double数组存放矩阵中的各元素

public:
matrix(short rows,short cols);
~matrix();

//重载运算符“()”,用来返回元素值
double operator() (short row,short col);
//给元素赋值
void setelem(short row,short col,double val);
//重载运算符“+”,实现距阵相加
friend matrix operator + (matrix p,matrix q);

//输出矩阵中的个元素
void print();
};

//定义构造函数
matrix::matrix(short rows, short cols)
{
matrix::rows = rows;
matrix::cols = cols;
elems = new double[rows * cols];
}

//定义析构函数
inline matrix::~matrix()//inline表示函数是内涵的
{
delete elems;
}



double matrix::operator()(short row,short col)
{
return ((row>=1 && row<=rows && col>=1 && col<=cols) ? elems[(row-1)*cols+(col-1)]:0.0);
}

//为矩阵赋值
void matrix::setelem(short row,short col,double val)
{
if(row>=1 && row<=rows && col>=1 && col<=cols)
elems[(row-1)*cols + (col-1)]=val;//数组顺序存放,计算出元素下标
}

//“+”
matrix operator + (matrix p, matrix q)
{
matrix m(p.rows,q.cols);
if(p.rows != q.rows || p.cols !=q.cols)
return m;
for(int r=1; r<=p.rows; r++)
for(int c=1; c<=p.cols; c++)
m.setelem(r,c,(p(r,c) + q(r,c)));
return m;
}



//输出元素各值
void matrix::print()
{
for(int r=1; r<=this->rows; ++r)
{
for(int c=1; c<=this->cols; c++)
cout<<(*this)(r,c)<<" ";
cout<<"\n";//调用重载运算符“()”
}
}

//主函数
void main()
{
matrix a(2,3),b(2,3),d(2,3);//创建矩阵对象
a.setelem(1,1,1.0);
a.setelem(1,2,2.0);
a.setelem(1,3,3.0);
a.setelem(2,1,4.0);
a.setelem(2,2,5.0);
a.setelem(2,3,6.0);
b.setelem(1,1,1.0);
b.setelem(1,2,2.0);
b.setelem(1,3,3.0);
b.setelem(2,1,4.0);
b.setelem(2,2,5.0);
b.setelem(2,3,6.0);


d=a+b;
d.print();

}
...全文
40 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
zxm954712 2002-11-18
  • 打赏
  • 举报
回复
还有,我改的程序是两个链接起来,才行啊,
如下:
#include <iostream.h>
//定义矩阵类
class matrix;

matrix *m;
class matrix
{
short rows, cols;//表示矩阵的行、列
double * elems;//double数组存放矩阵中的各元素

public:
matrix(short rows,short cols);
matrix(const matrix &other);
~matrix();

//重载运算符“()”,用来返回元素值
double operator() (short row,short col);
//给元素赋值
void setelem(short row,short col,double val);
//重载运算符“+”,实现距阵相加
friend const matrix &operator + (const matrix &p,const matrix &q);
matrix & operator=(const matrix &other);
//输出矩阵中的个元素
void print();
};

matrix::matrix(const matrix &other)
{
elems = new double[other.cols*other.rows];
cout << "elems is " << elems << endl;
for(int r=1; r<=other.rows; r++)
for(int c=1; c<=other.cols; c++)
setelem(r, c, ((matrix &)other)(r,c));

}

matrix & matrix::operator =(const matrix &other)
{
if (this == &other)
return *this;

cout << "elems is " << elems << endl;
delete [] elems;

elems = new double[other.cols*other.rows];
cout << "elems is " << elems << endl;
for(int r=1; r<=other.rows; r++)
for(int c=1; c<=other.cols; c++)
setelem(r, c, ((matrix &)other)(r,c));

return *this;
}
//定义构造函数
matrix::matrix(short rows, short cols)
{
matrix::rows = rows;
matrix::cols = cols;
elems = new double[rows * cols];
}

//定义析构函数
inline matrix::~matrix()//inline表示函数是内涵的
{
delete elems;
}



double matrix::operator()(short row,short col)
{
return ((row>=1 && row<=rows && col>=1 && col<=cols) ? elems[(row-1)*cols+(col-1)]:0.0);
}

//为矩阵赋值
void matrix::setelem(short row,short col,double val)
{
if(row>=1 && row<=rows && col>=1 && col<=cols)
elems[(row-1)*cols + (col-1)]=val;//数组顺序存放,计算出元素下标
}

//“+”
const matrix &operator + (const matrix &p,const matrix &q)
{
m = new matrix(p.rows,q.cols);
if(p.rows != q.rows || p.cols !=q.cols)
return *m;
for(int r=1; r<=p.rows; r++)
for(int c=1; c<=p.cols; c++)
m->setelem(r,c,(((matrix &)p)(r,c) + ((matrix &)q)(r,c)));
return *m;
}



//输出元素各值
void matrix::print()
{
for(int r=1; r<=this->rows; ++r)
{
for(int c=1; c<=this->cols; c++)
cout<<(*this)(r,c)<<" ";
cout<<"\n";//调用重载运算符“()”
}
}

//主函数
void main()
{
matrix a(2,3),b(2,3),d(2,3);//创建矩阵对象
a.setelem(1,1,1.0);
a.setelem(1,2,2.0);
a.setelem(1,3,3.0);
a.setelem(2,1,4.0);
a.setelem(2,2,5.0);
a.setelem(2,3,6.0);
b.setelem(1,1,1.0);
b.setelem(1,2,2.0);
b.setelem(1,3,3.0);
b.setelem(2,1,4.0);
b.setelem(2,2,5.0);
b.setelem(2,3,6.0);


d=a+b;
d.print();
delete m;

}
zxm954712 2002-11-18
  • 打赏
  • 举报
回复
析构函数是要的,因为你用new分配了内存空间,你必须要用delete释放掉内存空间啊,不然会引起内存泄漏啊!
win32c 2002-11-16
  • 打赏
  • 举报
回复
问题我已经找到,不过还是要谢谢大家!!
其实,只要删掉析构函数就可以了!!
win32c 2002-11-15
  • 打赏
  • 举报
回复
非常感谢!!可结果怎么是这样的???
elems is 0x00441E90
elems is 0x00441E90
2 4 -6.27744e+066
-6.27744e+066 -6.27744e+066 -6.27744e+066
Press any key to continue
zxm954712 2002-11-15
  • 打赏
  • 举报
回复
for(int r=0; r<other.rows; r++)
for(int c=0; c<other.cols; c++)
setelem(r, c, ((matrix &)other)(r,c));
改成
for(int r=1; r<=other.rows; r++)
for(int c=1; c<=other.cols; c++)
setelem(r, c, ((matrix &)other)(r,c));
zxm954712 2002-11-15
  • 打赏
  • 举报
回复
改成一下,试试
#include <iostream.h>
//定义矩阵类
class matrix;

matrix *m;
class matrix
{
short rows, cols;//表示矩阵的行、列
double * elems;//double数组存放矩阵中的各元素

public:
matrix(short rows,short cols);
matrix(const matrix &other);
~matrix();

//重载运算符“()”,用来返回元素值
double operator() (short row,short col);
//给元素赋值
void setelem(short row,short col,double val);
//重载运算符“+”,实现距阵相加
friend const matrix &operator + (const matrix &p,const matrix &q);
matrix & operator=(const matrix &other);
//输出矩阵中的个元素
void print();
};

matrix::matrix(const matrix &other)
{
elems = new double[other.cols*other.rows];
cout << "elems is " << elems << endl;
for(int r=1; r<=other.rows; r++)
for(int c=1; c<=other.cols; c++)
setelem(r, c, ((matrix &)other)(r,c));

}

matrix & matrix::operator =(const matrix &other)
{
if (this == &other)
return *this;

cout << "elems is " << elems << endl;
delete [] elems;

elems = new double[other.cols*other.rows];
cout << "elems is " << elems << endl;
for(int r=0; r<other.rows; r++)
for(int c=0; c<other.cols; c++)
setelem(r, c, ((matrix &)other)(r,c));

return *this;
}
//定义构造函数
matrix::matrix(short rows, short cols)
{
matrix::rows = rows;
matrix::cols = cols;
elems = new double[rows * cols];
}

//定义析构函数
inline matrix::~matrix()//inline表示函数是内涵的
{
delete elems;
}



double matrix::operator()(short row,short col)
{
return ((row>=1 && row<=rows && col>=1 && col<=cols) ? elems[(row-1)*cols+(col-1)]:0.0);
}

//为矩阵赋值
void matrix::setelem(short row,short col,double val)
{
if(row>=1 && row<=rows && col>=1 && col<=cols)
elems[(row-1)*cols + (col-1)]=val;//数组顺序存放,计算出元素下标
}

//“+”
const matrix &operator + (const matrix &p,const matrix &q)
{
m = new matrix(p.rows,q.cols);
if(p.rows != q.rows || p.cols !=q.cols)
return *m;
for(int r=1; r<=p.rows; r++)
for(int c=1; c<=p.cols; c++)
m->setelem(r,c,(((matrix &)p)(r,c) + ((matrix &)q)(r,c)));
return *m;
}



//输出元素各值
void matrix::print()
{
for(int r=1; r<=this->rows; ++r)
{
for(int c=1; c<=this->cols; c++)
cout<<(*this)(r,c)<<" ";
cout<<"\n";//调用重载运算符“()”
}
}

//主函数
void main()
{
matrix a(2,3),b(2,3),d(2,3);//创建矩阵对象
a.setelem(1,1,1.0);
a.setelem(1,2,2.0);
a.setelem(1,3,3.0);
a.setelem(2,1,4.0);
a.setelem(2,2,5.0);
a.setelem(2,3,6.0);
b.setelem(1,1,1.0);
b.setelem(1,2,2.0);
b.setelem(1,3,3.0);
b.setelem(2,1,4.0);
b.setelem(2,2,5.0);
b.setelem(2,3,6.0);


d=a+b;
d.print();
delete m;

}

你需要添加赋值函数和拷贝构造函数,不难会引起数据成员elems被释放两次,引起错误。 呵呵
nobounded 2002-11-14
  • 打赏
  • 举报
回复
friend matrix operator + (matrix p,matrix q);
改为:friend matrix& operator + (matrix& p,matrix& q)
当然:用成成员函数可能好一点。
ginger 2002-11-14
  • 打赏
  • 举报
回复
重载+那里有问题。
继续关注。
win32c 2002-11-14
  • 打赏
  • 举报
回复
大家说的办法我都用了,可问题还没解决!!
能不能帮调试一下呀!!
linfei2707 2002-11-14
  • 打赏
  • 举报
回复
你这里错就错在下面一句上

friend matrix operator + (matrix p,matrix q);

这里不能用传值,因为传值copy 时,elems不会重新new 一个,

d=a+b;时
在 matrix operator + (matrix p, matrix q)里
a.elems == p.elems ,b.elems == q.elems
在函数结束是就已经 delete 了a.elems 和b.elems;

main函数结束析构a, b时就会出错

win32c 2002-11-14
  • 打赏
  • 举报
回复
指针释放不是问题!我想问题可能在相加后的赋值上,我试着重载”=“问题还是一样!!
sukersoft 2002-11-14
  • 打赏
  • 举报
回复
晕,线性数学中的问题?
feng234 2002-11-14
  • 打赏
  • 举报
回复
不好意思,我还真没有找到,我编译了一下,没有发现问题!
kxw 2002-11-14
  • 打赏
  • 举报
回复
//重载运算符“+”,实现距阵相加
friend matrix operator + (matrix p,matrix q);//这样效率太低了,传引用

在构造函数中写的new double[rows * cols];
而在析构函数中delete elems;有memory leak;
写为delete[] elems;

而且,我一直对operator()在这里的用法感到怀疑,一般来讲,matrix类应该用代理类来实现,把每一行设为一个类。作为一个matrix类的private成员。

仅供参考

69,336

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧