一个我研究了很多天,却不能解决的问题!关于不等二维数组之间的赋值(使用运算符重载)

michaeldg 2007-04-29 01:20:43
下面是源代码,用vc6.0编译通过,但运行时总是提示赋值操作有问题(我用运算符重载实现的)!
首先是头文件
#ifndef ARRAY_H
#define ARRAY_H


#include <iostream.h>



class DoubleSubscriptedArray
{

friend ostream &operator<< (ostream &, const DoubleSubscriptedArray &);
friend istream &operator>> (istream &, DoubleSubscriptedArray &);


int **ptr;

int size;
int y;
int x;



public:
DoubleSubscriptedArray( int , int );

~DoubleSubscriptedArray();
int access(int ,int) const;
const DoubleSubscriptedArray &operator= (const DoubleSubscriptedArray &);
int operator== (const DoubleSubscriptedArray &) const;
int operator!= (const DoubleSubscriptedArray &) const;
int &operator [] (int);






};


#endif

//然后是函数的定义部分
//注意:重载的=有问题,关键问题就在 重载的‘=’上。

#include <iostream.h>
#include <stdlib.h>
#include <assert.h>
#include "array.h"


DoubleSubscriptedArray:: DoubleSubscriptedArray(int m,int n)
{
x=m;
y=n;
size =x*y;



ptr=new int *[x];
for(int j=0;j<y;j++)
ptr[j]=new int[y];

/* assert(ptr!=0);
for(int k=0;k<m;k++)
for(int l=0;l<n;l++)
ptr [k][l] =0;
*/



}


DoubleSubscriptedArray::~DoubleSubscriptedArray()
{
for(int h =0; h < x; h++)
delete []ptr[h];
delete []ptr;


}

int DoubleSubscriptedArray:: access(int a,int b) const
{

return ptr[a][b];

}

int DoubleSubscriptedArray:: operator== (const DoubleSubscriptedArray &right) const
{

if(size!=right.size || x!=right.x || y!=right.y)
return 0;

for(int i=0;i<x;i++)
for(int j=0;j<y;j++)
if(ptr[i][j] != right.ptr[i][j])
return 0;

return 1;

}

int DoubleSubscriptedArray::operator != (const DoubleSubscriptedArray &right) const
{
if(size!=right.size || x!=right.x || y!=right.y)
return 1;

for (int i=0;i<x;i++)
for(int j=0; j<y; j++)
if(ptr[i][j]!= right.ptr[i][j])
return 1;

return 0;

}

//问题基本上出在这块里面:
//我苦想研究了许多天也没有解决

const DoubleSubscriptedArray &DoubleSubscriptedArray:: operator= (const DoubleSubscriptedArray &right)
{
if(&right!= this) {
for(int h =0; h < x; h++)
delete [] ptr[h];
delete [] ptr;

x=right.x;
y=right.y;

ptr=new int*[right.x];
for(int k=0; k<right.x; k++)
ptr[k] = new int[right.y];


// assert(ptr!=0);

for(int i=0;i<x;i++)
for(int j=0;j<y;j++)

ptr[i][j] = right.ptr[i][j];
}

return *this;

}




istream &operator>> (istream &input, DoubleSubscriptedArray &a)
{
for(int i=0;i<a.x;i++)
for(int j=0;j<a.y;j++)
input>>a.ptr[i][j];
return input;
}

ostream &operator<< (ostream &output, const DoubleSubscriptedArray &a)
{
for(int i=0;i<a.x;i++)
for(int j=0;j<a.y;j++)
{
output<<a.ptr[i][j]<<' ';

if(j==(a.y-1))
output<<endl<<" ";
}


return output;

}

//下面就是测试主程序啦
//大家看看!



#include <iostream.h>
#include "array.h"


void main()
{
DoubleSubscriptedArray a1(2,3),a2(2,2);
cout<<"input 10 intgers"<<endl;
cin>>a1>>a2;
cout<<"a1:"<<a1<<endl;
cout<<"a2:"<<a2<<endl;
cout<<"a2(0,0)="<<a2.access(0,0)<<endl;
if(a1!=a2)
cout<<"the are not equal!"<<endl;

a1=a2;
cout<<"a1:"<<a1<<endl;
cout<<"a2:"<<a2<<endl;
if(a1==a2)
cout<<"the are equal!"<<endl;


}

各位兄弟,可以将代码运行检查一下,我研究一周!我实在没有办法弄出问题在哪里?
跪求高手!必将分送给解答正确的那位!
...全文
290 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
michaeldg 2007-04-29
  • 打赏
  • 举报
回复
最后一位高手的第一个改动才是问题的关键!
lightnut 2007-04-29
  • 打赏
  • 举报
回复
class DoubleSubscriptedArray
{
.......

const DoubleSubscriptedArray &operator= (const DoubleSubscriptedArray &);

////////====> DoubleSubscriptedArray &operator= (const DoubleSubscriptedArray &);
......................

};
.............
DoubleSubscriptedArray:: DoubleSubscriptedArray(int m,int n)
{
x=m;
y=n;
size =x*y;

ptr=new int *[x];
for(int j=0;j<y;j++) //////=====> for (int j=0; j<x; ++j)
ptr[j]=new int[y];
...........................
}

const DoubleSubscriptedArray &DoubleSubscriptedArray:: operator=
/////====>DoubleSubscriptedArray &DoubleSubscriptedArray:: operator=
(const DoubleSubscriptedArray &right)
{
if(&right!= this) {
for(int h =0; h < x; h++)
delete [] ptr[h];
delete [] ptr;

x=right.x;
y=right.y;

//////====> size = rhs.size;
.............


}

yutaooo 2007-04-29
  • 打赏
  • 举报
回复
我觉得 size 的概念用成员函数来表达更合适。

int DoubleSubscriptedArray::size() const {
return x * y;
}
jixingzhong 2007-04-29
  • 打赏
  • 举报
回复
否则,在operator== 中
if(size!=right.size || x!=right.x || y!=right.y)

第一个条件分支就不会成立
(除非一开始就是 == 的)
jixingzhong 2007-04-29
  • 打赏
  • 举报
回复
在 operator= 中:

x=right.x;
y=right.y;
size=right.size; //增加这个语句
yutaooo 2007-04-29
  • 打赏
  • 举报
回复
const DoubleSubscriptedArray &operator= (const DoubleSubscriptedArray &);

改成

DoubleSubscriptedArray & operator= (const DoubleSubscriptedArray &);

65,213

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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