-2

我有这个保存值的向量(不在堆上!)

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;

怎么了??

4

1 回答 1

0

这是移动任务,所以移动你的对象:

open = std::move(other.open);
menu = std::move(other.menu);

open或者menu可能不允许复制,因此出现错误。

您不需要 clear menu,因为您正在用另一个对象替换内容。

于 2018-11-14T17:03:59.440 回答