[英]Copy assignment operator, vector
我有這個保存值的向量(不在堆上!)
std::vector<Dish> menu;
我想實現這樣的復制賦值運算符:
Restaurant &Restaurant::operator=(Restaurant &&other) {
if (this == &other)
return *this;
open = other.open;
menu = std::move(other.menu);
}
我收到這些錯誤/警告:
^
/Users/avivlevitzky/CLionProjects/SPL-Project-1/Restaurant.cpp:49:10: note: in instantiation of member function 'std::__1::vector<Dish, std::__1::allocator<Dish> >::operator=' requested here
menu = other.menu;
^
/Users/avivlevitzky/CLionProjects/SPL-Project-1/Dish.h:18:15: note: copy assignment operator of 'Dish' is implicitly deleted because field 'id' is of const-qualified type 'const int'
const int id;
怎么了??
這是移動分配,所以移動你的對象:
open = std::move(other.open);
menu = std::move(other.menu);
open
或menu
可能不允許被復制,因此出現錯誤。
您不需要清除menu
,因為您正在用另一個對象替換內容。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.