0

I have a function that is to remove an object from a list based on it's name, which is returned through the class function special_event.getName(). It takes the list and the name of the object it is to remove, and after removing it, it is to return the list with the object removed. Here is the code:

list<special_event> removeEvent(string eventName, list<special_event> events)
{
list<special_event>::iterator eit;
string getName;
for (eit = events.begin(); eit != events.end(); eit++)
{
    if ((*eit).getName() == eventName)
    {
        events.remove(*eit);
    }
}
cout << endl << endl << eventName << " has been removed!";
return events;

}

I get the error "error C2678: binary '==' : no operator found which takes a left-hand operand of type 'special_event' (or there is no acceptable conversion).

Any help would be appreciated, thanks

Jacob Oaks
  • 140
  • 2
  • 11

3 Answers3

3

Missing operator==

You need to implement a proper bool operator==(...) overload. I'd tell you the arguments but it's not clear from what you've shown in the question.

Iterator invalidation

Once you call events.remove(*eit);, eit is no longer valid. So eit++ causes undefined behavior.

You already have an iterator

Since you already have an iterator to the element you are removing, why not use it with std::list::erase which takes an iterator argument.

Bill Lynch
  • 80,138
  • 16
  • 128
  • 173
1

Your special_event class/struct needs to have the right == overload (You may have overloaded it in a way where the left-hand type is something which is not special_event or can be converted to it).

bool operator==(const special_event&);

If the operator overload is a member function.

a_pradhan
  • 3,285
  • 1
  • 18
  • 23
0

if ((*eit).getName() == eventName)

You're asking for a comparison on your type special_event. The compiler can't possibly know how to compare this new type to anything. Therefore you have to overload the == operator to define how the comparison is made.

bool operator==(const special_event& right_side){      
    /* comparison */ 
}