0

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);
    }
}
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
drakerc
  • 139
  • 8
  • 4
    Do you have a function that can compare two `Books` to see if they are equal? – NathanOliver May 26 '16 at 11:40
  • 1
    Show us `Books` code. Have you overloaded `operator==` for the same? – CinCout May 26 '16 at 11:40
  • 1
    How to overload `operator==` https://stackoverflow.com/questions/4421706/operator-overloading – Cory Kramer May 26 '16 at 11:40
  • Thank you, I looked at my overload operator and it was certainly faulty; I just corrected it and it seems to be working properly (I added the code as an answer below). One more question: what access should be set to the overload operator? Should it be public or protected or something else? – drakerc May 26 '16 at 12:12

2 Answers2

1

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.

eerorika
  • 232,697
  • 12
  • 197
  • 326
0

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;
}
};
drakerc
  • 139
  • 8