简体   繁体   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;
}

This is the bookDetails function of my Administration class. 这是我的Administration类的bookDetails功能。 It loops through and asks for a books title, author and ISBN number and when its finished it pushes the book onto a list. 它循环并询问书籍标题,作者和ISBN号,当它完成时,它将书推到列表上。

This seems to work fine when i debug it but i get an error when i try to print out the details of each book using the iterator. 当我调试它时,这似乎工作正常,但当我尝试使用迭代器打印出每本书的详细信息时,我得到一个错误。

Can anyone help me out here? 有人可以帮我从这里出去吗? Thanks 谢谢

You need to implement operator<< for Book 您需要实现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