【问题标题】:user defined variables on the heap getting destroyed in while loop堆上的用户定义变量在while循环中被破坏
【发布时间】:2013-11-25 03:44:41
【问题描述】:

我是一个奇怪的问题,如果有人能指出我正确的方向,我将不胜感激。我在堆上创建了自己的链表变量,但是当我向其中添加另一个变量时,它们都会被破坏我不知道为什么。 在我的主目录中,我像这样设置我的变量
ma​​in.cpp

 Book* temp;  
    temp = bookSetUp(); 

这会转到另一个名为 functions 的 cpp,它会像这样设置对象:
functions.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;  
}

这很好用,但后来当我再次在主目录中添加到列表时,使用:
ma​​in.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() 函数,因为我觉得这可能是我遗漏了一些小东西,或者只是犯了一个灾难性的错误。我的 add 函数在 functions.cpp 中,我知道所有链接都在工作,因为除了这个之外我还有其他所有东西都在运行
functions.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;  
}

我的用户定义的类工作得很好,只是当我去添加我的列表被破坏的任何想法??

【问题讨论】:

  • add 还是append?你能准备一个minimal complete example吗?
  • 我认为我们需要更多的 main.
  • 顺便说一句,这不是你的错误,但 Book* append(Book* tempParam) 在你传递一个空列表时不起作用。你应该解决这个问题。
  • 您将 tempParam 设置为 list 并在您证明(通过 while 循环测试)list 为 NULL 之后立即返回它。
  • main 已相应更新,我将修复 @RichardPlunkett

标签: c++ function linked-list heap-memory


【解决方案1】:

*我认为错误是在这部分代码中找到的:

 list->setNext(temp);  

您正在“丢失”这些书籍,因为您在更改列表之前没有保存它们->next.*

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

【讨论】:

  • 我也这么认为,但是当我调试并返回主目录时,列表仍然存在,直到它“完成”循环,此时列表被销毁@AFM
  • @Dennington-bear - 所以当它退出附加功能时,列表仍然包含所有书籍?
  • 是的。我刚刚在那里检查过,现在由于某种原因它可以完美运行,我不知道为什么,因为我没有改变任何东西!令人费解的@AFM 感谢您的帮助哦,实际上我确实有一个问题,尽管看到作者在 append() 中的输入?如果我输入“阿尔伯特爱因斯坦”有什么理由会打印“阿尔伯特爱因斯坦”
  • @Dennington-bear - 它与 cin.ignore() 有关。我得查一下它的作用。
  • 列表->setNext(temp);检查 list->getNext() 为 NULL 后正在做,那这怎么会丢书呢??
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-10-09
  • 2015-05-01
  • 2016-01-12
  • 2018-06-18
  • 2016-02-19
  • 2018-03-21
  • 1970-01-01
相关资源
最近更新 更多