深拷贝不成功

Deep copy unsuccessful

这可能是一个愚蠢的问题,如果是,请告诉我,我会尽快将其删除。问题是我必须在 class "Kambarys" 中进行深度复制(忽略混合语言,我知道我不应该那样做)。程序在第二次尝试调用函数后终止。问题可能是我在构造函数副本中的语法,但我无法在任何地方找到正确的语法。其中一项要求是使用“new”在动态内存中创建 langas、durys 和 kambarys,并在 Kambarys 析构函数中删除 windows vector 和 door。感谢帮助! 要求: 在 main 方法中,使用 new 运算符创建房间 k1,并向其添加 windows 和门。编写一个构造函数 Room (const Room & k) 来创建一个正确的副本。在方法main中,写另一个房间k2。计算底板/墙面积的长度。 执行以下步骤:k2 = * k1;删除 k1;

#include <iostream>
#include <vector>
#include <iomanip>

using namespace std;

class Langas{
    private:
    float height;
    float widht;
    static int countL;
    public:
    Langas(float h, float w){
        this->height=h;
        this->widht=w;
        countL++;
    }
    ~Langas(){
        --countL;
    }
    float getHeight(){
      return height;
    }
    float getWidht(){
      return widht;
    }
    static int getWindowCount(){
        return countL;
    }
};

class Durys{
    private:
    float heightD;
    float widhtD;
    static int countD;
    public:
    Durys(float hD, float wD){
        this->heightD=hD;
        this->widhtD=wD;
        countD++;
    }
    ~Durys(){
        --countD;
    }
    float getHeightD(){
      return heightD;
    }
    float getWidhtD(){
      return widhtD;
    }
    static int getDoorCount(){
        return countD;
    }

};
class Kambarys{
    private:    
    float heightK;
    float widhtK;
    float lenghtK;
    public:
    vector<Langas*> windows;
    Durys* door;
    Kambarys(float hK, float wK, float lK){
        this->heightK=hK;
        this->widhtK=wK;
        this->lenghtK=lK;
    }
    Kambarys(const Kambarys &k){
        this->door=k.door;
        this->windows=k.windows;
        heightK=k.heightK;
        widhtK=k.widhtK;
        lenghtK=k.lenghtK;
    }
    ~Kambarys(){
        door=NULL;
        for(int i=0; i<windows.size(); i++){
            delete windows[i];
        }
        windows.clear();
        delete door;
    }
    float getHeightK(){
      return heightK;
    }
    float getWidhtK(){
      return widhtK;
    }
    float getLenghtK(){
      return lenghtK;
    }
    void addWindow(Langas* w){
        windows.push_back(w);
    }
    void addDoor(Durys *d){
        door=d;
    }
};
float countWallPlot(Kambarys* k){
    float cWPlot=(2*k->getLenghtK()*k->getHeightK())+(2*k->getWidhtK()*k->getHeightK());
    for(int i=0; i<k->windows.size(); i++){
        cWPlot-=((k->windows[i]->getHeight()))*(k->windows[i]->getWidht());
    }
    cWPlot-=((k->door->getHeightD()))*(k->door->getWidhtD());
    return cWPlot;
}
float countLenght(Kambarys* k){
    float floorL=(k->getLenghtK()*k->getWidhtK()*2);
    floorL-=(k->door->getWidhtD());
    return floorL;
}
int Langas::countL=0;
int Durys::countD=0;

int main(){
    Langas *langas1=new Langas(3.4, 1.2);
    Durys *durys=new Durys(3.1, 1.5);
    Langas *langas2=new Langas(6.4, 1.5);
    Kambarys *k=new Kambarys(30.4, 40.1, 50.1);
    Kambarys *k2=k;
    k->addWindow(langas1);
    k->addWindow(langas2);
    k->addDoor(durys);
    cout<<countWallPlot(k)<<" "<<countLenght(k)<<endl;
    cout<<"Window count "<<Langas::getWindowCount()<<", door count "<<Durys::getDoorCount()<<endl;
    k2=k;
    delete k;
    cout<<countWallPlot(k2)<<" "<<countLenght(k2)<<endl;
    cout<<"Window count "<<Langas::getWindowCount()<<", door count "<<Durys::getDoorCount()<<endl;
}

您必须为 k2 分配内存并复制对象,而不是指针。

您必须在复制构造函数和复制赋值运算符中分配内存。

door=NULL;delete door; 之前会跳过 delete 并导致内存泄漏。

windows.clear(); 在析构函数中不是必需的。保持代码简单。

编辑:添加 " 后执行以下步骤:k2 = * k1; delete k1;" 我创建了 k2 对象,而不是指针。

#include <iostream>
#include <vector>

class Langas {
private:
  float height;
  float width;
  static int count;

public:
  Langas(float h, float w): height(h), width(w) {
    ++count;
  }
  ~Langas() { --count; }
  float getHeight() const { return height; }
  float getWidht() const { return width; }
  static int getWindowCount() { return count; }
};

class Durys {
private:
  float height;
  float width;
  static int count;

public:
  Durys(float h, float w): height(h), width(w) {
    ++count;
  }
  ~Durys() { --count; }
  float getHeight() const { return height; }
  float getWidth() const { return width; }
  static int getDoorCount() { return count; }
};

class Kambarys {
private:
  float height;
  float width;
  float length;

public:
  std::vector<Langas *> windows;
  Durys *door = nullptr;
  Kambarys(float hK, float wK, float lK): height(hK), width(wK), length(lK) {}
  Kambarys(const Kambarys &k): height(k.height), width(k.width), length(k.length), windows(), door(k.door ? new Durys(k.door->getHeight(), k.door->getWidth()) : nullptr) {
    for (const auto window : k.windows) {
      windows.emplace_back(new Langas(window->getHeight(), window->getWidht()));
    }
  }
  Kambarys &operator=(const Kambarys &k) {
    door = k.door ? new Durys(k.door->getHeight(), k.door->getWidth()) : nullptr;
    for (const auto window : k.windows) {
      windows.emplace_back(new Langas(window->getHeight(), window->getWidht()));
    }
    height = k.height;
    width = k.width;
    length = k.length;
    return *this;
  }
  ~Kambarys() {
    for (auto window : windows) {
      delete window;
    }
    delete door;
  }
  float getHeight() const { return height; }
  float getWidth() const { return width; }
  float getLength() const { return length; }
  void addWindow(Langas *w) { windows.emplace_back(w); }
  void addDoor(Durys *d) { door = d; }
};

float countWallPlot(const Kambarys &k) {
  float cWPlot = 2 * k.getLength() * k.getHeight() + 2 * k.getWidth() * k.getHeight();
  for (const auto window : k.windows) {
    cWPlot -= window->getHeight() * window->getWidht();
  }
  cWPlot -= k.door->getHeight() * k.door->getWidth();
  return cWPlot;
}

float countLength(const Kambarys &k) {
  float floor = k.getLength() * k.getWidth() * 2;
  floor -= k.door->getWidth();
  return floor;
}

int Langas::count = 0;
int Durys::count = 0;

int main() {
  Langas *langas1 = new Langas(3.4, 1.2);
  Durys *durys = new Durys(3.1, 1.5);
  Langas *langas2 = new Langas(6.4, 1.5);
  Kambarys *k = new Kambarys(30.4, 40.1, 50.1);
  Kambarys k2(*k);
  k->addWindow(langas1);
  k->addWindow(langas2);
  k->addDoor(durys);
  std::cout << countWallPlot(*k) << " " << countLength(*k) << std::endl;
  k2 = *k;
  std::cout << "Window count " << Langas::getWindowCount() << ", door count " << Durys::getDoorCount() << std::endl;
  delete k;
  std::cout << countWallPlot(k2) << " " << countLength(k2) << std::endl;
  std::cout << "Window count " << Langas::getWindowCount() << ", door count " << Durys::getDoorCount() << std::endl;
}