这样用动态二维指针为什么不对
#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;
} |