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 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器