张传旭 2016-04-11 09:13 采纳率: 92.3%
浏览 1585
已采纳

大神帮着看一下,为什么加上delete[]arr,这句就会显示错误,不加就没事

#include
#include
using namespace std;
class Array
{
private:
int **arr;
int n,m;
public:
Array(int nn, int mm);
Array(const Array &stc);
~Array();
friend Array operator+(Array a1, Array a2);
friend ostream& operator<<(ostream &,Array &a);
friend istream& operator>>(istream &, Array &a);
Array operator=(Array a1);
};
Array::Array(int nn, int mm)
{
n = nn;
m = mm;
arr = new int [nn];
for (int i = 0; i < m; i++)
{
arr[i] = new int[mm];
}
}
Array::Array(const Array &stc)
{
m = stc.m;
n = stc.n;
arr = new int
[n];
for (int i = 0; i < m; i++)
{
arr[i] = new int[m];
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
arr[i][j] = stc.arr[i][j];
}
}
}
Array::~Array()
{
for (int i = 0; i < n; i++)
{
delete[]arr[i];
}
//delete[] arr; //加上这句会出错,为什么????。不加就会正常运行
}
Array operator+(Array a1, Array a2)
{
Array a(a1.n, a1.m);
for (int i = 0; i < a1.n; i++)
{
for (int j = 0; j < a1.m; j++)
{
a.arr[i][j] = a1.arr[i][j] + a2.arr[i][j];
}
}
return a;
}
Array Array::operator=(Array a1)
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
arr[i][j] = a1.arr[i][j];
}
}
return *this;
}
istream& operator>>(istream & input, Array &a)
{

for (int i = 0; i <a.n ; i++)
{
    for (int j = 0; j < a.m; j++)
    {
        input >> a.arr[i][j];
    }
}
return input;

}
ostream& operator<<(ostream & output, Array &a)
{

for (int i = 0; i <a.n; i++)
{
    for (int j = 0; j < a.m; j++)
    {
        cout.width(3);
        output << a.arr[i][j];
    }
    output << endl;
}
return output;

}
void main()
{
int n, m;
cin >> n >> m;
Array arr1(n, m), arr2(n, m), arr3(n, m);
cin >> arr1;
cin >> arr2;
arr3 = arr1 + arr2;
cout << arr1<<endl;
cout << arr2 << endl;
cout << arr3 << endl;
system("pause");

}

  • 写回答

2条回答

  • lxffff1990 2016-04-11 09:34
    关注
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler