简体   繁体   English

将unique_ptr传递给对象会引发错误

[英]passing unique_ptr to object throws error

I am learning builder design pattern and smart pointer. 我正在学习构建器设计模式和智能指针。
So I am trying to use smart_ptr all the way in my code. 因此,我尝试在代码中完全使用smart_ptr。 But when I create instance in main using unique_ptr and pass it to Contractor object it throws error. 但是,当我使用unique_ptr在main中创建实例并将其传递给Contractor对象时,它将引发错误。

If I replaces unqiue_ptr with shared_ptr in contractor class as well as in main. 如果我在承包商类和main类中用shared_ptr替换unqiue_ptr。 The prog runs fine but no destructor is called. 该编运行正常,但未调用任何析构函数。

Below are the errors I get when I use unique_ptr. 以下是我使用unique_ptr时遇到的错误。

 error: call to implicitly-deleted copy constructor of 'std::unique_ptr<HouseBuilder>'
 Contractor *ctr1 = new Contractor(lavishHouseBldr);

/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/memory:2621: copy constructor is implicitly deleted because 'unique_ptr<HouseBuilder, std::__1::default_delete<HouseBuilder> >' has a user-declared move constructor
_LIBCPP_INLINE_VISIBILITY unique_ptr(unique_ptr&& __u) _NOEXCEPT
                              ^
passing argument to parameter 'houseBuilder' here
 Contractor(std::unique_ptr<HouseBuilder>houseBuilder): houseBuilder(std::move( houseBuilder))

Below are my code.. 下面是我的代码。

/* Concrete class for the HousePlan interface */
class House
{
private:
  std::string  window, door, bathroom, floor, kitchen;
public:
  void setWindow( string window)
  {
      this->window = window;
  }
  void setDoor( string door)
  {
      this->door = door;
  }
  ~House()
  {
      cout <<"destructor  House called"<<endl;
  }
};
/* Builder class*/
class HouseBuilder
{
public:
    virtual void buildWindow()   = 0;
    virtual void buildDoor()     = 0;
    virtual House* getHouse()    = 0;
    virtual ~HouseBuilder() = 0;
};
HouseBuilder::~HouseBuilder()
{
    std::cout <<"Destructor for housebuilder called";
}
class LavishHouse:public HouseBuilder
{
public:
  LavishHouse( )
  {
      house.reset(new House());
  }
  ~LavishHouse( )
  {
      cout <<"Lavish House Destructor callled"<<std::endl;
  }
  void buildWindow()
  {
     house->setWindow(" French Windows");
  }

  void buildDoor()
  {
      house->setDoor(" woodenDoor");
  }
  House* getHouse()
  {
      return  house.get();
  }
private:
    std::unique_ptr<House> house;
};
/* The Director. Consturct director*/

class Contractor
{
public:
    Contractor(std::unique_ptr<HouseBuilder>houseBuilder): houseBuilder(std::move( houseBuilder))
    {

    }
   /* Contractor(HouseBuilder*houseBuilder): houseBuilder(houseBuilder)
    {

    }*/
    ~Contractor()
    {
        cout <<"destruct contractor"<<std::endl;
    }

    House *getHouse()
     {
      return houseBuilder->getHouse();
     }

     void buildHouse()
     {
      houseBuilder->buildWindow();
      houseBuilder->buildDoor();
     }
private:
    std::unique_ptr<HouseBuilder> houseBuilder;

};

/* Example on how to use the Builder design pattern */
int main()
{

std::unique_ptr< HouseBuilder> lavishHouseBldr( new LavishHouse() );
 Contractor *ctr1 = new Contractor(lavishHouseBldr); // error!!!!!

 ctr1->buildHouse();
 House *house1 = ctr1->getHouse();
 cout<<"Constructed: "<<house1<< std::endl;
 return 0;
}

lavishHouseBldr is an lvalue, which can't be moved; lavishHouseBldr是一个左值,不能移动; You need to apply std::move on lavishHouseBldr too, ie 您还需要在lavishHouseBldr std::move上应用lavishHouseBldr ,即

Contractor *ctr1 = new Contractor(std::move(lavishHouseBldr));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM