簡體   English   中英

打印列表中的元素

[英]Printing out elements in a list

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;
}

這是我的Administration類的bookDetails功能。 它循環並詢問書籍標題,作者和ISBN號,當它完成時,它將書推到列表上。

當我調試它時,這似乎工作正常,但當我嘗試使用迭代器打印出每本書的詳細信息時,我得到一個錯誤。

有人可以幫我從這里出去嗎? 謝謝

您需要實現operator<< for Book

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM