gpt4 book ai didi

c++ - 打印列表中的元素

转载 作者:行者123 更新时间:2023-11-28 03:03:50 25 4
gpt4 key购买 nike

Book Administrator::bookDetails()
{
Book book;
list<Book> books;

string title;
string author;
int ISBN;

string userInput;

while (userInput != "q")
{
cout << "Would you like to enter a book?" << endl;
cin >> userInput;
if (userInput == "yes")
{
cout << "What is the title of the book you want to enter?" << endl;
cin >> title;

cout << "What is the author of the book you want to enter?" << endl;
cin >> author;

cout << "What is the ISBN of the book you want to enter?" << endl;
cin >> ISBN;

book.setTitle(title);
book.setAuthor(author);
book.setISBN(ISBN);

books.push_back(book);

}

list<Book>::iterator pos;
pos = books.begin();

for (pos = books.begin(); pos != books.end(); pos++)
{
//There error is produced here
cout << *pos << "\n";
}

}
return book;
}

这是我的管理类的 bookDetails 函数。它循环遍历并询问书名、作者和 ISBN 号,完成后将书推送到列表中。

这在我调试时似乎工作正常,但当我尝试使用迭代器打印每本书的详细信息时出现错误。

有人可以帮我吗?谢谢

最佳答案

您需要实现 operator<<对于 Book

std::ostream operator<<(std::ostream& os, const Book& book)
{
os << book.title << " " << book.author; // print out other information
return os;
}

关于c++ - 打印列表中的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20156352/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com