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_)
所以我尝试重载 == 运算符和 * 运算符,但无法解决我的问题,没有找到我做错的地方
在查找我是否调试时我认为问题是
find(_InputIterator __first, _InputIterator __last, const _Tp& __value_)
{
for (; __first != __last; ++__first)
if (*__first == __value_)-------------> issue seems to be hereis here
break;
return __first;
}
因为我重载了 == 运算符,所以比较应该发生在 int 和 int 之间,我不明白为什么在 book 对象和 int 之间发生比较。请帮忙
如果您想通过 id 查找一本书,您可以使用
std::find_if
和 lambda 表达式
auto itr = find_if(
m_books.begin(),
m_books.end(),
[=](const Book& b) { return b.getBookId() == booid; }
);