gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 07:16:22 24 4
gpt4 key购买 nike

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

 Book* temp;  
temp = bookSetUp();

这会转到另一个称为函数的 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;
}

这很好用,但后来当我在主要使用中再次添加到列表时:
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() 函数,因为我觉得它可能是我遗漏了一些小东西或者只是犯了一个灾难性的错误。我的添加函数在 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);  

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

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

关于c++ - 堆上的用户定义变量在 while 循环中被销毁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20184396/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com