堆上的用户定义变量在while循环中被销毁

user defined variables on the heap getting destroyed in while loop

本文关键字:循环 while 用户 定义 变量      更新时间:2023-10-16

我有一个奇怪的问题,我无法理解,如果有人能给我指明正确的方向,我将不胜感激。我在堆上创建了自己的链表变量,但当我添加另一个变量时,它们都会被销毁,我不知道为什么。在我的主体中,我设置了这样的变量
main.cpp

 Book* temp;  
    temp = bookSetUp(); 

这将转到另一个名为函数的cpp,该cpp设置如下对象:
函数.cpp

      Book* bookSetUp()  
        {  
    //The items that populate the list  
    Book* a= new Book("A Tale of Two Cities", "Charles Dickens", "1", true);  
    Book* b= new Book("Lord of the rings", "J.R.R Tolkein", "2", true);  
    Book* c= new Book("Le Petit Prince", "Antoine de Saint-Exupéry", "3", true);  
    Book* d= new Book("And Then There Were None", "Agatha Christie", "4", true);  
    Book* e= new Book("Dream of the Red Chamber","Cao Xueqin","5", true);  
    Book* f= new Book("The Hobbit","J.R.R Tolkein","6", true);  
    //sets up the pointers between books  
    a->setPrev(NULL);  
    a->setNext(b);  
    b->setPrev(a);  
    b->setNext(c);  
    c->setPrev(b);  
    c->setNext(d);  
    d->setPrev(c);  
    d->setNext(e);  
    e->setPrev(d);  
    e->setNext(f);  
    f->setPrev(e);  
    f->setNext(NULL);  
    //sets up a temp pointer to a  
    Book* temp = a;  
    //returns the temp pointer to a  
    return temp;  
}

这非常有效,但稍后当我再次添加到列表时,主要使用:
main.cpp

else if(checkRegUser(username, password, regUserList) == true)
    {
        int choice = 99;
        cout << "Welcome Registered user: "<< username << endl;
        while(choice != 0)
        {
            //this is so the print will start everytime as if you run it once print will be at NULL thereafter
            Book* print = temp;
            choice = options();
            if(choice == 1)
            {
                while(print!=NULL)
                {
                        cout<<"Name: "<<print->getName()<<endl<<"Author: "<<print->getAuthor()<<endl<<"ISBN: "<<print->getISBN()<<endl<<"Availability: "<<print->getAvail()<<endl;
                        cout<<endl;
                        print = print->getNext();
                }
                print = temp;
            }
            if(choice == 2)
            {
                search(temp);
            }
            if(choice == 3)
            {
                takeOut(temp);
            }
            if(choice == 4)
            {
                returnBack(temp);
            }
            if(choice == 5)
            {
                append(temp);
            }
            if(choice == 6)
            {
                cout<<"Sorry you have the privilege needed to use this function."<<endl;
            }
            if(choice == 7)
            {
                choice = 0;
            }
        }
    }

我的用户定义的变量被销毁了。我调试了一下,它们就消失了,我不知道为什么
以防万一,这里需要我的add()函数,因为我觉得这可能是我遗漏了一些小东西,或者只是犯了一个灾难性的错误。我的添加函数在functions.cpp中,我知道所有的链接都在工作,因为除此之外,我还有其他一切都在运行
函数.cpp

Book* append(Book* tempParam)  
{  
   string title;  
   string author;  
   string isbn;  
   bool avail;  
   cout<<"What is the book called?"<<endl;  
   cin.clear();  
   cin.ignore();  
   getline(cin, title);     
   cout<<"Who is the author?"<<endl;  
   cin.clear();  
   cin.ignore();  
   getline(cin, author);  
   cout<<"What is the ISBN to be?"<<endl;  
   cin>>isbn;  
   Book* temp = new Book(title, author, isbn, true);
   Book* list = tempParam;int count;  
   while(list!=NULL)  
   {  
      if(list->getNext()==NULL&&list->getName()!=title)  
      {
         list->setNext(temp);  
         temp->setNext(NULL);  
         temp->setPrev(list);  
         cout<<"Your book has been added"<<endl;  
         cout<<temp->getName()<<temp->getAuthor()<<endl;  
      }  
      list = list->getNext();  
   }  
   tempParam = list;  
   return tempParam;  
}

我的用户定义类工作得很好——就在我添加的时候,我的列表被破坏了——有什么想法吗??

*我认为错误出现在代码的这一部分:

 list->setNext(temp);  

你正在"丢失"这些书,因为你在更改列表->下一个列表之前没有保存它们。*

答案不正确,因为条件语句确保它是列表的最后一个元素。很抱歉