2013-11-25 51 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容器,这意味着它会自动处理动态分配。

如果使用动态分配的数组这是不同的...


我想你的列表中“消失”,是因为来宾和管理员有两个完全不同的列表。

要解决此问题,您应该授予管理员访问权限的书籍列表访问权限。

或者提取出书单并将其存储在另一个允许访问列表中的管理员和访客的类中。