Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/137.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 我的列表对象正在被销毁_C++_List_Function_Pointers - Fatal编程技术网

C++ 我的列表对象正在被销毁

C++ 我的列表对象正在被销毁,c++,list,function,pointers,C++,List,Function,Pointers,这是我的Guest类,我想在这里搜索我在Administrator类中创建的书籍列表,但当它进入函数printBookDetails时,我的列表不包含任何元素,我假设它们已被销毁 void Guest::searchBook(Book* search) { string searchBook; cout << "What book would you like to search for?" << endl; cin >> search

这是我的Guest类,我想在这里搜索我在Administrator类中创建的书籍列表,但当它进入函数printBookDetails时,我的列表不包含任何元素,我假设它们已被销毁

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

    printBookDetails();
}
My Guest类从管理员类继承printBookDetails

所有的管理功能都可以工作,但是一旦它到达guest,元素就会消失


有什么我可以解决的吗?非常感谢您的帮助

首先,您不必动态创建列表对象

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();
你这样做是因为

你的责任更少:你不必发布书籍中包含的资源。它将自动释放

列表是一个stl容器,这意味着它会自动处理动态分配

如果使用动态分配的数组,则情况有所不同

我认为你们的名单不见了是因为来宾和管理员有两个完全不同的名单

要解决此问题,您应该将管理员拥有的图书列表的访问权授予您的来宾


或者提取图书列表并将其存储在另一个类中,该类允许管理员和来宾访问该列表。

多亏了@OldProgrammer,我意识到我必须从Admin类返回图书列表,并将其作为参数传递给来宾类中的printBookDetails方法:

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

这还不清楚。您没有展示admin1和guest1是如何创建的,也没有展示类声明。这里已经有很多代码,我只是不想把所有的东西都扔掉。我只是在main中创建它们,现在就编辑它。guest1是一个单独的对象,与admin1对象没有关系。为什么您认为admin1中的列表应该提供给guest1?老实说,我只是在学习,这是我的第一个大项目。我不确定情况是否如此。那么,我没有办法从guest类访问该列表吗?那么,添加一个方法从Admin类返回列表,并将其作为参数传递给guest.printBookDetails方法。更好的方法是通过const引用传入列表,这样可以防止guest修改列表,除非您希望修改它
list<Book> books = new list<Book>();
//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);