1
list<Book*> books;
list<Book>::iterator pos, last;

Book Administrator::addBook()
{
    Book *newBook = new Book();
    cout << "Would you like to enter a book?" << endl;
    cin >> userInput;
    cout << endl;

    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;

        cout << endl;

        newBook->setTitle(title);
        newBook->setAuthor(author);
        newBook->setISBN(ISBN);
        newBook->setAvailability(true);

        books.push_back(newBook);

    }
    return *newBook;
}

void Administrator::printBookDetails()
{

    books.begin()->setPrevBook(NULL);
    for (pos = books.begin(); pos != books.end(); ++pos)
    {
        cout << pos->getTitle() << "\n"
            << pos->getAuthor() << "\n"
            << pos->getISBN() << "\n"
            << pos->getAvailability() << "\n"
            << "******************************" << endl;

        if (pos != books.begin())
        {
            last->setNextBook(&*pos);
            pos->setPrevBook(&*last);
        }
        last = pos;
    }
    books.back().setNextBook(NULL);

}

Can someone help me finish off this project please, these are my two functions addBook and printbookDetails . These are in my Admin class.

I would like the books i create on the heap to be stored in list< book*> books as i want to reference them in another class.

I've gotten a bit of help on here so far in regards to pointers and i know its something to do with me not linking the pointers to the correct objects.

My printBookDetails is giving me trouble, the first line books.begin()->setPrevBook(NULL); is saying i need a pointer-to-class type but when i put the -> i still have an error.


Book Guest::searchBook(Book* search)
{

    string searchBook;
    cout << "What book would you like to search for?" << endl;
    cin >> searchBook;

    printBookDetails();


}

What id like to do is use the searchBook function above in my Guest class to reference the books in the list at a later time but when i cannot seem to get my pointers right.

Can someone please put me on the right track.

3
  • Canyou tell me what books.begin()->setPrevBook(NULL) is for? Commented Nov 24, 2013 at 23:32
  • It looks a little bit like you are trying to connect up all your books into a double-linked list, whose nodes are Book objects. And that you are doing this within the Books held by a std::list<>. Is there some reason you arent just using the std::list, all the other setNextBook/setPrevBook stuff is likely to cause you trouble. As for the line itself, begin() is a like a pointer to an item, just like pos, so see me answer below for fix. Commented Nov 24, 2013 at 23:37
  • Sure, our lecturer wants us to have a link for all books, so when starting the iteration, i link the first element of the list to NULL Commented Nov 24, 2013 at 23:37

1 Answer 1

2
for (pos = books.begin(); pos != books.end(); ++pos)

Here, pos is an iterator, over your list. Iterators are like pointers to list entries. But each of your list entries is also a pointer to a book, not a book itself. So, you need code like: (*pos)->getTitle() when trying to access member functions for those elements.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.