首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

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

2012-03-03 
一个我研究了很多天,却不能解决的问题!关于不等二维数组之间的赋值(使用运算符重载)下面是源代码,用vc6.0

一个我研究了很多天,却不能解决的问题!关于不等二维数组之间的赋值(使用运算符重载)
下面是源代码,用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;  


}

各位兄弟,可以将代码运行检查一下,我研究一周!我实在没有办法弄出问题在哪里?
跪求高手!必将分送给解答正确的那位!


[解决办法]
const DoubleSubscriptedArray &operator= (const DoubleSubscriptedArray &);



改成

DoubleSubscriptedArray & operator= (const DoubleSubscriptedArray &);
[解决办法]
在 operator= 中:

x=right.x;
y=right.y;
size=right.size; //增加这个语句
[解决办法]
否则,在operator== 中
if(size!=right.size || x!=right.x || y!=right.y)

第一个条件分支就不会成立
(除非一开始就是 == 的)
[解决办法]
我觉得 size 的概念用成员函数来表达更合适。

int DoubleSubscriptedArray::size() const {
return x * y;
}
[解决办法]
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;
.............


}

热点排行