0

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?

Community
  • 1
  • 1
TheWaterProgrammer
  • 7,055
  • 12
  • 70
  • 159

1 Answers1

3

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.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483