简体   繁体   中英

Copy assignment operator, vector

I have this vector that hold values (Not on the Heap!)

std::vector<Dish> menu;

I want to implement copy assignment operator like this:

    Restaurant &Restaurant::operator=(Restaurant &&other) {
    if (this == &other)
        return *this;
    open = other.open;

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

I get those error/warnings:

        ^
/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;

What's wrong??

It's the move assignment, so move your objects:

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

open or menu probably don't allow to be copied, hence the error.

You don't need to clear menu , because you are replacing the content with another object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM