Reputation: 139
I'm wondering how can I remove an object from a C++ list - I'm trying to create a book management software and I'd like to have a function in the database (which is a list) that removes a book from it which is a passed object. I was thinking about this solution, but it won't work - seems like the == operator is not properly overloaded? Or maybe this solution is not going to work?
class Database
{
list <Books> mybooks;
public:
void BookDel(Books & temp)
{
mybooks.remove(temp);
}
}
Upvotes: 0
Views: 73
Reputation: 139
My == overload was wrong, so I corrected it and it seems to be working now. This is my Books class code:
class Book {
public: bool operator==(const Book & a) const
{
bool test=false;
if(!(this->tytul.compare(a.tytul)))
test=true;
return test;
}
protected:
list <Autor> authors;
string tytul;
public:
void AddAuthor(Autor x)
{
authors.push_back(x);
}
Books(string tytulx)
{
tytul = tytulx;
}
};
Upvotes: 0
Reputation: 238311
Or maybe this solution is not going to work?
This solution should work, as long as Book
objects are comparable with the operator==
.
but it won't work - seems like the == operator is not properly overloaded?
If the operator isn't properly overloaded, then that could certainly be the problem. You should research more in depth how it does not work. That will give you insight about why it does not work, which will lead you to the solution.
Upvotes: 1