I am on C++
using std::vector
to store a list of class objects using vector::push_back
.
My add function accepts the base class type so that the same method can be used across.
Add(MyBaseClass object) {
my_vector.push_back(object)
}
I have remove function to remove it by per item added
Remove(MyBaseClass object) {
my_vector.erase(std::remove(my_vector.begin(), my_vector.end(), object), my_vector.end());
}
The add works fine but my Remove method gives following error:
overload resolution selected deleted operator '=='
if (!(*__i == __value_))
~~~~ ^ ~~~~~~~~
I picked up the vector::erase code from here . What is wrong with my way of erasing an item? Are there any other preferable ways to erase by item?
The Multipass guarantee of the ForwardIterator
requirement for remove
to work on std::vector
specifies that MyBaseClass
must implement operator==
.
Your class doesn't appear to implement this, and compilation therefore fails.
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.