2013-11-25 84 views
0
list<Book> *books = new list<Book>; 

    list<Book>::iterator pos; 

    void 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); 

     } 
    } 

***************** 

我真的需要這方面的幫助,今天我收到了一些答案,但沒有人幫我解決了這個問題。這是我的admin類。它在堆上創建的書籍和將它們存儲在一個stl::list我的清單物品正在銷燬

void Guest::searchBook(Book* search) 
{ 
    string searchBook; 
    cout << "What book would you like to search for?" << endl; 
    cin >> searchBook; 

    printBookDetails(); 
} 

這是我Guest類,我想在這裏做的是通過書籍我在Administrator類創建的列表來搜索,但當它進入功能printBookDetails,我的列表中不包含任何元素,我假設它們已被銷燬。

Administrator* admin1 = new Administrator("jayfitz91", 24681357); 
Guest* guest1 = new Guest("guest", 0000); 

void main() 
{ 

    //Everything here works fine 
    admin1->addBook(); 
    admin1->addBook(); 
    admin1->makeAvailable(); 
    admin1->printBookDetails(); 

    //My list is destroyed at this point and it returns nothing 
    guest1->printBookDetails(); 

Guest類從我Administrator

所有的管理職能的工作,但只要它到達的客人,元素消失繼承printBookDetails

無論如何,我可以解決這個問題嗎?該幫助將不勝感激

+0

這是不明確。您不會顯示admin1和guest1如何創建或類聲明。 – OldProgrammer

+0

這裏已經有很多代碼了,我只是不想把所有東西都扔掉。我只是在主體中創建它們,現在編輯它 – user2757842

+2

guest1是一個單獨的對象,並且與admin1對象沒有任何關係。爲什麼您認爲admin1中的列表應該可用於guest1? – OldProgrammer

回答

0

感謝@OldProgrammer,我意識到我不得不從我Admin類返回圖書列表,並通過把它作爲參數傳遞給我的printBookDetails方法在我的來賓類:

//Admin class 
//Now returning a list 
list<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 *books; 
} 



//Guest class function 

void Guest::printBookList(list<Book> *tempList) 
{ 
    pos = tempList->begin(); 
    for (pos = tempList->begin(); pos != tempList->end(); ++pos) 
    { 
     cout << pos->getTitle() << "\n" 
      << pos->getAuthor() << "\n" 
      << pos->getISBN() << "\n" 
      << pos->getAvailability() << "\n" 
      << "******************************" << endl; 

    } 

} 

//Main class function 

    //Create a temporary variable for the list 
    list<Book> tempList; 

    //pass it through to the method 
    guest1->printBookList(&tempList); 
+0

更好的方法是通過const引用傳遞列表,阻止Guest方式修改列表(除非您希望修改) – sellsword

-1

首先,您不必動態創建您的列表對象。

list<Book> books = new list<Book>(); 

你這樣做是因爲

  1. 你有較少的責任:你不必以釋放書籍包含的資源。它會自動發佈。

  2. list是一個stl容器,這意味着它會自動處理動態分配。

如果使用動態分配的數組這是不同的...


我想你的列表中「消失」,是因爲來賓和管理員有兩個完全不同的列表。

要解決此問題,您應該授予管理員訪問權限的書籍列表訪問權限。

或者提取出書單並將其存儲在另一個允許訪問列表中的管理員和訪客的類中。