将std :: unique_ptr传递给类时出错

国民党

我必须创建从抽象类继承的类的实例。我的代码很简单。它应该基于抽象类创建对象类的实例。抽象类也是模板类。然后,我需要将此对象放入保存指向该对象的指针的存储类中。传递指针时出现错误:

templates.cpp: In member function ‘void storage::setPTR(std::unique_ptr<child>&)’:
templates.cpp:39:28: error: use of deleted function ‘std::unique_ptr<_Tp, _Dp>& std::unique_ptr<_Tp, _Dp>::operator=(const std::unique_ptr<_Tp, _Dp>&) [with _Tp = child; _Dp = std::default_delete<child>]’
             this->childPTR = pointer;
                            ^
In file included from /usr/include/c++/5/memory:81:0,
                 from templates.cpp:3:
/usr/include/c++/5/bits/unique_ptr.h:357:19: note: declared here
       unique_ptr& operator=(const unique_ptr&) = delete;
                   ^
templates.cpp: In function ‘int main()’:
templates.cpp:45:30: error: type/value mismatch at argument 1 in template parameter list for ‘template<class _Tp, class _Dp> class std::unique_ptr’
     std::unique_ptr<INTERFACE> p = std::make_unique<child>("xxx");
                              ^
templates.cpp:45:30: note:   expected a type, got ‘INTERFACE’
templates.cpp:45:30: error: template argument 2 is invalid
templates.cpp:45:65: error: cannot convert ‘std::_MakeUniq<child>::__single_object {aka std::unique_ptr<child>}’ to ‘int’ in initialization
     std::unique_ptr<INTERFACE> p = std::make_unique<child>("xxx");
                                                                 ^
templates.cpp:48:24: error: ‘newChild’ was not declared in this scope
     testStorage.setPTR(newChild);
                        ^

我的代码:

#include <iostream>
#include <string>
#include <memory>

// using namespace std;

template<typename type1, typename type2, typename type3> class INTERFACE {
    protected:
        type1 x;
        type2 y;
        type3 name;

    public:
        virtual type1 setX(type1 x) = 0;
        virtual type2 setY(type2 y) = 0;
};

class child : public INTERFACE<int, float, std::string> {
    public:
        child(std::string z) {
            this->name = z;
        }

        virtual int setX(int x) override {
            this->x = x;
        } 

        virtual float setY(float y) override {
            this->y = y;
        }
};

class storage {
    private:
        std::unique_ptr<child> childPTR;

    public:
        void setPTR(std::unique_ptr<child> & pointer) {
            this->childPTR = pointer;
        }
};

int main(){
    // std::unique_ptr<INTERFACE> newChild(new child("xxx"));
    std::unique_ptr<INTERFACE> p = std::make_unique<child>("xxx");

    storage testStorage;
    testStorage.setPTR(newChild);

    return 0;
}

我做错了什么?

逻辑资料
  1. std::unique_ptr不可复制分配。它是可移动分配的。因此,请在参数和std::move使用值传递setPTR

  2. 您需要提供一个具体的类型,而不是模板:

    std::unique_ptr<INTERFACE<int, float, std::string>> p =
        std::make_unique<child>("xxx");
    
  3. 声明newChild,在这种情况下,您将再次需要将std::move其放入函数或发出该变量。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何将std :: unique_ptr传递给函数

如何将包含 std::unique_ptr 的 std::optional 结构传递给函数?

声明包含std :: unique_ptr的结构的std :: vector类时出错

如何将 unique_ptr 对象作为参数传递给作为库的类

将std :: unique_ptr传递给构造函数以获取所有权

使用已使用 new X() 创建的变量将 std::unique_ptr 传递给函数

将unique_ptr <Derived>&传递给接受unique_ptr <Base>&的函数

将unique_ptr引用传递给boost :: bind?

如何将类的静态方法作为 std::unique_ptr 的删除器传递?

将std :: unique_ptr类成员标记为const

weak_ptr与unique_ptr参考-将接口impl传递给其他对象

使用std :: function对象将自定义删除器传递给std :: unique_ptr

将带有unique_ptr的可变lambda传递给const&std :: function

使用 std::move 将 std::unique_ptr 作为 qt 信号参数传递

是否可以将这个Lambda传递给`unique_ptr`终生可用?

如何将NULL或nullptr传递给接收unique_ptr参数的函数?

如何将unique_ptr参数传递给构造函数或函数?

将unique_ptr的向量传递给对象。向量成为成员变量。正确的方法?

C ++:将诸如unique_ptr :: get()之类的参数传递给函数是否安全?

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

将 unique_ptr 传递给完成处理程序时提升 ASIO“错误地址”错误

将引用参数传递给采用 unique_ptr 的通用引用的函数

将std :: unique_ptr插入std :: set

将 ptr 传递给返回的 std::string

将std :: unique_ptr推回std :: vector时,编译器不会失败

将main的args传递给类方法时出错

从std:unique_ptr隐式转换为布尔值时出错

从初始化列表构造std :: map <T,unique_ptr <S >>时出错

将std :: unique_ptr <Derived>转换为std :: unique_ptr <Base>