简体   繁体   English

这个错误是什么意思?” 在模板中:调用 &#39;std::unique_ptr 的已删除构造函数<InventoryElements> &quot;

[英]What does it means this error?" In template: call to deleted constructor of 'std::unique_ptr<InventoryElements>"

this is the class with all the methods.这是包含所有方法的类。

#include "InventoryElements.h"
class Inventory{
public:
    explicit Inventory(int max) : MaxElements(max){
        myInventory.resize(max);
        NumElements=0;
    }

    void suf_insert(unique_ptr<InventoryElements> element);
    void generic_insert(unique_ptr<InventoryElements> element, vector<  unique_ptr<InventoryElements>>::iterator pos);

    void remove(unique_ptr<InventoryElements> element,vector< unique_ptr<InventoryElements>>::iterator pos);

private:
    int MaxElements;
    vector<unique_ptr<InventoryElements>> myInventory;
    int NumElements;
};
void Inventory::suf_insert(std::unique_ptr<InventoryElements> element) {
    if (NumElements < MaxElements){
        myInventory.push_back(element); // here the error
        
    else
        std::cout<<"Ops.. your Inventory is full, remove an element first"<< endl;
}

and this is the implementation of the first method, a suf_insert, the error call to deleted constructor of 'std::unique_ptr",这是第一个方法的实现,suf_insert,对“std::unique_ptr”的已删除构造函数的错误调用,

how can i solve this?我该如何解决这个问题?

Because a std::unique_ptr can not be copied , you will need to move your unique_ptr into your vector.因为无法复制std::unique_ptr ,所以您需要将 unique_ptr移动到向量中。

    myInventory.push_back( std::move(element) ); // no more error

After this line, element will no longer point to your element, as your std::vector will now contain the unique pointer.在此行之后, element将不再指向您的元素,因为您的std::vector现在将包含唯一指针。

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

相关问题 错误:调用 &#39;std::__1::unique_ptr 的隐式删除复制构造函数<A, std::__1::default_delete<A> &gt;&#39; - error: call to implicitly-deleted copy constructor of 'std::__1::unique_ptr<A, std::__1::default_delete<A> >' 在 unique_ptr 中删除的构造函数 - Constructor deleted in unique_ptr 向量 <unique_ptr<A> &gt;在构造函数中-错误:调用隐式删除的拷贝构造函数 - vector<unique_ptr<A> > in constructor - error: call to implicitly-deleted copy constructor 在移动构造函数中的unique_ptr上调用std :: move时出现“错误:使用已删除的函数” - “error: use of deleted function” when calling std::move on unique_ptr in move constructor std :: unique_ptr和一个模板 - std::unique_ptr and a template 错误:使用auto调用unique_ptr的隐式删除的复制构造函数 - error: call to implicitly-deleted copy constructor of unique_ptr with auto class 中的 std::unique_ptr,试图引用已删除的 function 错误 - std::unique_ptr in class, attempting to reference a deleted function error 错误:使用已删除的 function std::unique_ptr - Error: Use of deleted function std::unique_ptr 错误:使用已删除的 function 'std::unique_ptr&lt;_Tp, _Dp&gt;::unique_ptr - error: use of deleted function ‘std::unique_ptr<_Tp, _Dp>::unique_ptr 带矢量的unique_ptr:错误:调用XXX的隐式删除副本构造函数 - unique_ptr with vector: error: call to implicitly-deleted copy constructor of XXX
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM