[英]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.