Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/145.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 深度复制失败_C++_Deep Copy - Fatal编程技术网

C++ 深度复制失败

C++ 深度复制失败,c++,deep-copy,C++,Deep Copy,可能是个愚蠢的问题,如果是,请告诉我,我会尽快删除它。问题是我必须在“Kambarys”课上做一个深度复制(忽略混合语言,我知道我不应该这么做)。程序在第二次尝试调用函数后终止。可能问题在于我在构造函数副本中的语法,但我在任何地方都找不到正确的语法。要求之一是使用“新建”在动态内存中创建langas、durys和kambarys,并在kambarys析构函数中删除windows向量和门。谢谢你的帮助! 要求: 在main方法中,使用新操作符创建房间k1,并向其中添加窗和门。编写一个可以创建正确副

可能是个愚蠢的问题,如果是,请告诉我,我会尽快删除它。问题是我必须在“Kambarys”课上做一个深度复制(忽略混合语言,我知道我不应该这么做)。程序在第二次尝试调用函数后终止。可能问题在于我在构造函数副本中的语法,但我在任何地方都找不到正确的语法。要求之一是使用“新建”在动态内存中创建langas、durys和kambarys,并在kambarys析构函数中删除windows向量和门。谢谢你的帮助! 要求: 在main方法中,使用新操作符创建房间k1,并向其中添加窗和门。编写一个可以创建正确副本的构造器室(constroom&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;
}
#包括
#包括
#包括
使用名称空间std;
兰加斯类{
私人:
浮动高度;
浮动宽度;
静态整数计数;
公众:
Langas(浮子h、浮子w){
这个->高度=h;
这个->宽=w;
countL++;
}
~Langas(){
--伯爵;
}
float getHeight(){
返回高度;
}
浮点getwidt(){
返乡;
}
静态int getWindowCount(){
返回计数;
}
};
杜里斯班{
私人:
漂浮高度;
浮动widhtD;
静态整数计数;
公众:
杜里斯(浮动hD、浮动wD){
这->高度d=hD;
这->widhtD=wD;
countD++;
}
~Durys(){
--倒计时;
}
float getHeightD(){
返回高度;
}
float getWidhtD(){
返回widhtD;
}
静态int getDoorCount(){
返回计数d;
}
};
坎巴里斯级{
私人:
浮动高度;
浮动widhtK;
浮动长度;
公众:
向量窗口;
杜里斯*门;
Kambarys(浮动hK、浮动wK、浮动lK){
这->高度k=hK;
这->widhtK=wK;
此->长度k=lK;
}
坎巴里斯(康斯特坎巴里斯和k){
此->门=k门;
此->窗口=k.windows;
heightK=k.heightK;
widhtK=k.widhtK;
lenghtK=k.lenghtK;
}
~Kambarys(){
门=零;
对于(inti=0;igetLenghtK()*k->getHeightK())+(2*k->getWidhtK()*k->getHeightK());
对于(int i=0;iwindows.size();i++){
cWPlot-=((k->windows[i]->getHeight())*(k->windows[i]->GetWidt());
}
cWPlot-=((k->door->getHeightD())*(k->door->getWidhtD());
返回图;
}
浮动计数长度(Kambarys*k){
float floorL=(k->getLenghtK()*k->getWidhtK()*2);
地板-=(k->door->getWidhtD());
返回地板;
}
int Langas::countL=0;
int Durys::countD=0;
int main(){
Langas*langas1=新的Langas(3.4,1.2);
杜里斯*杜里斯=新杜里斯(3.1,1.5);
Langas*langas2=新的Langas(6.4,1.5);
Kambarys*k=新Kambarys(30.4,40.1,50.1);
Kambarys*k2=k;
k->addWindow(langas1);
k->addWindow(langas2);
k->addDoor(杜里斯);

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


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

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

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

编辑:在添加“执行以下步骤:
k2=*k1;删除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;
}
#包括
#包括
兰加斯类{
私人:
浮动高度;
浮动宽度;
静态整数计数;
公众:
Langas(浮子h、浮子w):高度(h)、宽度(w){
++计数;
}
~Langas(){--count;}
float getHeight()常量{return height;}
float getwidt()常量{return width;}
静态int getWindowCount(){return count;}
};
杜里斯班{
私人:
浮动高度;
浮动宽度;
静态整数计数;
公众:
Durys(浮动h、浮动w):高度(h)、宽度(w){
++计数;
}
~Durys(){--count;}
float getHeight()常量{return height;}
float getWidth()常量{return width;}
静态int getDoorCount(){return count;}
};
坎巴里斯级{
私人:
浮动高度;
浮动宽度;
浮子长度;
公众:
std::向量窗口;
Durys*door=nullptr;
坎巴里斯(浮球hK,浮球wK,浮球lK):高度(hK),宽度(wK),长度(lK){}
Kambarys(const Kambarys&k):高度(k.height)、宽度(k.width)、长度(k.length)、窗()、门(k.door?新Durys(k.door->getHeight()、k.door->getWidth()):nullptr){
用于(常数自动窗口:k.windows){
重新放置(新的Langas(窗口->getHeight(),窗口->GetWidt());
}
}
Kambarys和operator=(常数Kambarys和k){
door=k.door?新DURY(k.door->getHeight(),k.door->getWidth()):空PTR;
用于(常数自动窗口:k.windows){
重新放置(新的Langas(窗口->getHeight(),窗口->GetWidt());
}
高度=k.高度;
宽度=k.宽度;
长度=k.长度;
归还*这个;
}
~Kambarys(){
用于(自动窗口:windows){
删除窗口;
}
删除门;
}
float getHeight()常量{return height;}
float getWidth()常量{return width;}
float getLength()常量{return length;}
void addWindow(Langas*w){windows.emplace_back(w);}
void addDoor(Durys*d){door=d;}
};
浮点数墙图(康斯特坎巴里斯和k){
float cWPlot=2*k.getLength()*k.getHeight()+2*k.getWidth()*k.getHeight();
用于(常数自动窗口:k.windows){
cWPlot-=窗口->获取高度()*窗口->获取宽度();
}
cWPlot-=k.door->getHeight()*k.door->getWidth();
返回图;
}
浮点计数长度(常数Kambarys&k){
浮动地板=k.getLength()*k.getWidth()*2;
地板-=k.door->getWidth();
返回层;
}
int Langas::count=0;
int Durys::count=0;
int main(){
Langas*langas1=新的Langas(3.4,1.2);
杜里斯*杜里斯=新杜里斯(3.1,1.5);
Langas*langas2=新的Langas(6.4,1.5