0
class book{
public:
   void setNameOfAuthor(string nameOfAuthor);
   string getNameOfAuthor();
   void setBookId(int bookid);
   int getBookId();
   bool operator==(const book& rhs)const;
   const int operator*()const;
private:
    string m_nameOfAuthor;
    int m_bookId;
};
class bms{
    private:
        vector<book> m_books;
    public:
        void addNewBook(book& booktoadd);
        void deleteBook();
        void displayBook();  
};
bool book::operator==(const book& rhs)const{
    return m_bookId == rhs.m_bookId;
}

const int book::operator*()const{
    return m_bookId;
}

void bms::deleteBook(){
    int booid;
    auto itr = find(m_books.begin(), m_books.end(), booid); ------>issue is here 
    if( itr != m_books.end()){
        m_books.erase(itr);
    }else{
        cout<<"book is not found!!"<<endl;
    }
}

im working on book management system project , trying to delete a book in find i get error as 
Library/Developer/CommandLineTools/SDKs/MacOSX11.3.sdk/usr/include/c++/v1/algorithm:919:22: error: invalid operands to binary expression ('book' and 'const int')
 if (*__first == __value_) 

so i tried overloading == operator and *operator but couldnt resolve my issue , not getting where im doing wrong

in find if i debug i think issue is

find(_InputIterator __first, _InputIterator __last, const _Tp& __value_)
{
    for (; __first != __last; ++__first)
        if (*__first == __value_)-------------> issue seems to be hereis here 
            break;
    return __first;
}

as i overloaded == operator so comparasion should happen between int and int im not getting why comparsion is happening between book object and int. please help

  • If you want to find a book by it's id use `std::find_if` not `std::find`. – john Aug 20 '23 at 14:01
  • `__first` is not a `book`, `*__first` is. – Evg Aug 20 '23 at 14:02
  • Use [`std::find_if`](https://en.cppreference.com/w/cpp/algorithm/find) with a suitable [lambda function](https://en.cppreference.com/w/cpp/language/lambda). – Some programmer dude Aug 20 '23 at 14:03
  • ***Or*** create an overload of `==` that compares `int` with `book` (and `book` with an `int`, yes order matters). I''d rather recommend the `std::find_if` and lambda solution though. – Some programmer dude Aug 20 '23 at 14:03
  • On the other hand, considering your use-case perhaps [`std::remove_if`](https://en.cppreference.com/w/cpp/algorithm/remove) would be a better solution? But for that please read more about [the erase-remove idiom](https://en.wikipedia.org/wiki/Erase%E2%80%93remove_idiom). – Some programmer dude Aug 20 '23 at 14:10
  • thank you so much for all the reply using find_if it works – farida fathima Aug 20 '23 at 19:54

3 Answers3

1

If you want to find a book by it's id, you can use std::find_if with a lambda expression

auto itr = find_if(
    m_books.begin(), 
    m_books.end(), 
    [=](const Book& b) { return b.getBookId() == booid; }
);
john
  • 85,011
  • 4
  • 57
  • 81
0

You are using an (uninitialized) int to search for a book but there is no operator== for comparing a book with an int.

You could define free operators for comparing a book with an int to solve the issue:

bool operator==(const book& b, int id) { return b.getBookId() == id; }
bool operator==(int id, const book& b) { return b == id; }
bool operator!=(const book& b, int id) { return !(b.getBookId() == id); }
bool operator!=(int id, const book& b) { return b != id; }

Another option is to actually create a book with the id you want to search for:

book bs;
bs.setBookId(booid); // note: booid must be initialized before this
auto itr = std::find(m_books.begin(), m_books.end(), bs);

A third option is to use std::find_if with a lambda as shown in john's answer.

Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
0

As I mentioned in my comment, for your use-case you should use std::remove_if instead of any "find" function:

m_books.erase(std::remove_if(m_books.begin(), m_books.end(), [booid](Book const& b)
{
    return b.getBookId() == booid);
}), m_books.end());

The above will remove all books matching booid from the container m_books.

Note that we need to use the erase-remove idiom to both remove the elements and erase unused elements after the removal.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621