qq_45735316 2020-05-15 10:08 采纳率: 94.1%
浏览 192
已采纳

为什么程序会在Point的析构函数里死循环???

图片说明图片说明
//classfile.h

//Pclassfile.h

#include <iostream>
using namespace std;
class Point {
public:
    Point() ;
    Point(int x, int y);
    ~Point() ;
    int getX()const;
    int getY()const;
    void move(int newX, int newY);
private:
    int x, y;
};
class ArrayOfPoints {
public:
    ArrayOfPoints(int size);              //构造函数
    ~ArrayOfPoints();                     //析构函数
    Point& element(int index);              //获得下表为index的数组元素
private:
    Point* points;                 //指向动态数组首地址
    int size;
};

//classfile.cpp

//Pclassfile.cpp
#include <iostream>
#include <cassert>
#include "classfile.h"
using namespace std;
Point::Point() :x(0), y(0) {
    cout << "default constructor called" << endl;
}
Point::Point(int x, int y) :x(x), y(y) {
    cout << "constructor called" << endl;
}
Point::~Point(){
    cout << "destructor called" << endl;
}
int Point::getX()const
{
    return x;
}
int Point::getY()const
{
    return y;
}
void Point::move(int newX, int newY)
{
    x = newX;
    y = newY;
}
ArrayOfPoints::ArrayOfPoints(int size) :size(size) 
{
    points = new Point[size];
}
ArrayOfPoints::~ArrayOfPoints()
{
    cout << "deleting..." << endl;
    delete[]points;
}
Point& ArrayOfPoints::element(int index)
{
    assert(index >= 0 && index < size);      //如果下标越界,程序终止
    return points[index];
}

//main.cpp

/*1.编译运行例6-21(对象的浅复制),
看看程序是否真的出错,
原因是什么?修改该程序,使其正确。*/
#include <iostream>
#include <cassert>
#include "classfile.h"
using namespace std;
int main()
{
    int count;
    cout << "please enter the count of points:" << endl;
    cin >> count;
    ArrayOfPoints pointsArray1(count);        //创建对象数组
    pointsArray1.element(0).move(5, 10);
    pointsArray1.element(1).move(15, 20);
    ArrayOfPoints pointArray2 = pointsArray1;
    cout << "copy of pointArray1:" << endl;
    cout << "Point_0 of array2:" << pointArray2.element(0).getX() << ","
        << pointArray2.element(0).getY() << endl;
    cout << "Point_1 of array2:" << pointArray2.element(1).getX() << ","
        << pointArray2.element(1).getY() << endl;
    pointsArray1.element(0).move(25, 30);
    pointsArray1.element(1).move(35, 40);
    cout << "After the moving of pointArray1:" << endl;
    cout << "Point_0 of array2:" << pointArray2.element(0).getX() << ","
        << pointArray2.element(0).getY() << endl;
    cout << "Point_1 of array2:" << pointArray2.element(1).getX() << ","
        << pointArray2.element(1).getY() << endl;
    return 0;
}
  • 写回答

1条回答 默认 最新

  • threenewbee 2020-05-15 10:21
    关注

    缺少拷贝构造函数,导致拷贝操作是浅拷贝,指针释放2次

    class ArrayOfPoints {
    public:
        ArrayOfPoints(int size);              //构造函数
        ~ArrayOfPoints();                     //析构函数
        Point& element(int index);              //获得下表为index的数组元素
            //加上拷贝构造函数
        ArrayOfPoints(ArrayOfPoints& aof)
        {
            points = new Point[aof.size];
            size = aof.size;
            for (int i = 0; i < aof.size; i++)
            {
                points[i] = aof.points[i];
            }
        }
    private:
        Point* points;                 //指向动态数组首地址
        int size;
    };
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

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