读书人

这样用动态二维指针为什么不对,该如何

发布时间: 2012-04-15 18:39:21 作者: rapoo

这样用动态二维指针为什么不对
#include <iostream.h>
class Douary
{
public:
Douary(int m=0, int n=0);//构造函数:用于建立动态数组存放m行n列的二维数组(矩阵)元素,并将该数组元素初始化为
~Douary(); //析构函数:用于释放动态数组所占用的存储空间。
friend istream &operator>>(istream &input, Douary &d);//重载运算符“>>”输入二维数组,其中d为Dousry类对象;
friend ostream &operator<<(ostream &output, Douary &d);//重载运算符“<<”以m行n列矩阵的形式输出二维数组,其中d为Douary类对象。



private:
int * Array; //Array 为动态数组指针。
int row; //row 为二维数组的行数。
int col; //col 为二维数组的列数。
};
Douary::Douary(int m, int n)//构造函数:用于建立动态数组存放m行n列的二维数组(矩阵)元素,并将该数组元素初始化为
{
row=m;
col=n;
int **Array = new int*[row];
for (int i = 0; i < row; i++)
{
Array[i] = new int[col];
}

for (int j = 0;j < row; j++)
{
for (int k = 0; k < col; k++)
{
Array[j][k] = (j + 1) * (k + 1);
}
}


}
istream &operator>>(istream &input, Douary &d)//重载运算符“>>”输入二维数组,其中d为Dousry类对象;
{
int **Array = new int*[d.row];
for (int i = 0; i < d.row; i++)
{
Array[i] = new int[d.col];
}

for (int j = 0;j < d.row; j++)
{
for (int k = 0; k < d.col; k++)
{
Array[j][k] = (j + 1) * (k + 1);
input>>Array[j][k];
//cout << Array[j][k] << &Array[j][k];
}
cout<<endl;
}
return input;
}
ostream &operator<<(ostream &output, Douary &d)//重载运算符“<<”以m行n列矩阵的形式输出二维数组,其中d为Douary类对象。
{
for (int j = 0;j < d.row; j++)
{
for (int k = 0; k < d.col; k++)
{
output << d.Array[j][k] <<'\t';
}
cout<<endl;
}
return output;
}
Douary::~Douary() //析构函数:用于释放动态数组所占用的存储空间。
{
delete []Array;

}
int main()
{
Douary d1(2,3),d2(2,3);
cout<<"输入d1:"<<endl;
cin>>d1;
cout<<"输入d2:"<<endl;
cin>>d2;
cout<<"d1="<<endl;
cout<<d1;
cout<<"d2="<<endl;
cout<<d2;
return 0;
}

[解决办法]

Assembly code
class Douary{public:    Douary(int m=0, int n=0);//构造函数:用于建立动态数组存放m行n列的二维数组(矩阵)元素,并将该数组元素初始化为    ~Douary(); //析构函数:用于释放动态数组所占用的存储空间。    friend istream &operator>>(istream &input, Douary &d);//重载运算符“>>”输入二维数组,其中d为Dousry类对象;    friend ostream &operator<<(ostream &output, Douary &d);//重载运算符“<<”以m行n列矩阵的形式输出二维数组,其中d为Douary类对象。    friend Douary &operator+(const Douary &d1,const Douary &d2);//两个矩阵相加,规则:对应位置上的元素相加    friend Douary &operator-(const Douary &d1,const Douary &d2);//两个矩阵相减,规则:对应位置上的元素相减    bool operator==(const Douary &d);//判断两个矩阵是否相等,即对应位置上的所有元素是否相等private:    int ** Array; //Array 为动态数组指针。    int row; //row 为二维数组的行数。    int col; //col 为二维数组的列数。};Douary::Douary(int m, int n)//构造函数:用于建立动态数组存放m行n列的二维数组(矩阵)元素,并将该数组元素初始化为{    row=m;    col=n;    if((m|n) != 0 )    {        Array=new int*[row];        for (int i=0;i<row;i++)        {            Array[i] = new int[col];        }    }    else        Array = NULL;}Douary::~Douary(){    if (Array != NULL)    {        for (int i=0;i<row;i++)        {            delete Array[i];        }        delete[] Array;        Array = NULL;    }}istream &operator>>(istream &input, Douary &d)//重载运算符“>>”输入二维数组,其中d为Dousry类对象;{    input>>d.row >>d.col ;    if((d.row|d.col) !=0)    {        int i,j;        d.Array=new int *[d.row];        for (i=0;i<d.row;i++)        {            d.Array[i] = new int[d.col];        }        for (i=0;i<d.row;++i)            for(j=0;j<d.col;++j)            {                input>>d.Array[i][j];            }//如果需要在>>中输入元素值就将该段生效即可。                }    else        d.Array = NULL;    return input;}ostream &operator<<(ostream &output, Douary &d)//重载运算符“<<”以m行n列矩阵的形式输出二维数组,其中d为Douary类对象。{    output<<d.row <<'/'<<d.col <<endl;     if ((d.row|d.col) !=0)    {        int i,j;        for(i=0;i<d.row;++i)        {            for (j=0;j<d.col;++j)                output<<d.Array[i][j]<<" ";            output<<endl;        }//如果需要在<<中输出元素值就将该段生效即可。    }    return output;}Douary &operator+(const Douary &d1,const Douary &d2)//两个矩阵相加,规则:对应位置上的元素相加{    Douary *p = new Douary(0,0);    if (d1.row==d2.row && d1.col==d2.col)    {        int i,j;        p->row = d2.row;        p->col = d2.col;        p->Array=new int *[d1.row];        for (i=0;i<d1.row;i++)        {            p->Array[i] = new int[d1.col];        }        for (i=0;i<d2.row;++i)            for(j=0;j<d2.col;++j)            {                p->Array[i][j] = d1.Array[i][j]+d2.Array[i][j];            }    }    else    {        cout<<"The dim of Matrix is not fixed!"<<endl;    }    return *p;}Douary &operator-(const Douary &d1,const Douary &d2)//两个矩阵相减,规则:对应位置上的元素相减{    Douary *p = new Douary(0,0);    if (d1.row==d2.row && d1.col==d2.col)    {        int i,j;        p->row = d2.row;        p->col = d2.col;        p->Array=new int *[d1.row];        for (i=0;i<d1.row;i++)        {            p->Array[i] = new int[d1.col];        }        for (i=0;i<d2.row;++i)            for(j=0;j<d2.col;++j)            {                p->Array[i][j] = d1.Array[i][j]-d2.Array[i][j];            }    }    else    {        cout<<"The dim of Matrix is not fixed!"<<endl;    }    return *p;}bool Douary::operator==(const Douary &d)//判断两个矩阵是否相等,即对应位置上的所有元素是否相等{    bool bRet = false;    if (row==d.row && col==d.col)    {        int i,j;        for(i=0;i<row;++i)            for(j=0;j<col;++j)                if(d.Array[i][j]!=Array[i][j])                    return false;        bRet = true;    }    return bRet;}int main(){    Douary d1(2,3),d2(2,3);        cout<<"输入d1:"<<endl;    cin>>d1;    cout<<"输入d2:"<<endl;    cin>>d2;    cout<<"d1="<<endl;    cout<<d1;    cout<<"d2="<<endl;    cout<<d2;    cout<<"d1+d2="<<endl;    cout<<(d1+d2);    cout<<"d1-d2="<<endl;    cout<<(d1-d2);    cout<<"d1"<<((d1==d2)?"==":"!=")<<"d2"<<endl;    return 0;} 

读书人网 >C++

热点推荐