【发布时间】:2013-11-25 03:44:41
【问题描述】:
我是一个奇怪的问题,如果有人能指出我正确的方向,我将不胜感激。我在堆上创建了自己的链表变量,但是当我向其中添加另一个变量时,它们都会被破坏我不知道为什么。
在我的主目录中,我像这样设置我的变量
main.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;
}
这很好用,但后来当我再次在主目录中添加到列表时,使用:
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() 函数,因为我觉得这可能是我遗漏了一些小东西,或者只是犯了一个灾难性的错误。我的 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