我有一个程序编译得很好,但当我尝试通过菜单运行函数时,我只收到分段错误的响应。我似乎也找不到seg故障发生在哪里。
linkedList.cpp
#include "linkedList.h"
#include <iostream>
#ifndef LINKEDLIST_CPP
#define LINKEDLIST_CPP
using namespace std;
//Destructor
template<class T>
linkedList<T>::~linkedList()
{
node *temp=new node;
while(head->!=NULL)
{
temp=head;
head=head->next;
delete temp;
}
}
//Copy Constructor
template <class T>
linkedList<T>::linkedList(const linkedList &list)
{
node *temp =new node;
temp =list.head;
delete this;
while(temp->next!=NULL)
{
orderedInsert(temp->data);
temp=temp->next;
}
}
//Assignment Operator
template <class T>
linkedList<T> &linkedList<T>::operator=(const linkedList &list)
{
if(this != &list)
{
current =head;
while(current->next!=NULL)
{
head=head->next;
delete current;
current=head;
}
current=list.head;
while(current->!=NULL)
{
orderedInsert(current->data);
current=current->next;
}
}
return *this;
}
template <class T>
bool linkedList<T>::empty() const
{
current=head;
if(current==NULL)
{
return true;
}
else
return false;
}
template <class T>
void linkedList<T>::clear()
{
delete *this;
}
template <class T>
bool linkedList<T>::search(const T &value)
{
current=head;
while(current->data!=value||current->!=NULL)
{
current=current->next;
}
if(current->data==value)
{
return true;
}
else
return false;
}
template <class T>
void linkedList<T>::orderedInsert(const T &value)
{
current=head;
while(value>current->data)
{
trailCurrent=current;
current=current->next;
}
trailCurrent->next=new node(value,current);
}
template <class T>
bool linkedList<T>::remove(const T &value)
{
node *temp;
temp =head;
if(search(value)!=true)
{
return false;
}
else
{
trailCurrent->next=current->next;
temp=current;
current=current->next;
delete temp;
return true;
}
}
template <class T>
bool linkedList<T>::replace(const T &oldData,const T &newData)
{
if(search(oldData)==false)
{
return false;
}
else
{
current=head;
while(current->data!=oldData)
{
current=current->next;
}
current->data=newData;
}
return true;
}
template <class T>
void linkedList<T>::insert(const T &value)
{
if(trailCurrent==NULL)
{
trailCurrent=head;
head =new node(value,trailCurrent);
trailCurrent=head;
}
else if(trailCurrent->next->data!=current->data)
{
trailCurrent=head;
while(trailCurrent->next->data!=current->data)
{
trailCurrent=trailCurrent->next;
}
node *temp;
temp=trailCurrent;
temp=new node(value,current);
trailCurrent->next=temp;
}
return true;
}
template <class T>
bool linkedList<T>::retrieve(T &value)const
{
if(current==NULL)
{
return false;
}
else
{
current->data =value;
return true;
}
}
template <class T>
void linkedList<T>::begin()
{
current=head;
trailCurrent=NULL;
}
template <class T>
linkedList linkedList<T>::operator++()
{
if(current!=NULL)
{
current=current->next;
trailCurrent=trailCurrent->next;
return *this;
}
}
template<class T>
linkedList linkedList<T>::operator++(int i)
{
if(current!=NULL)
{
trailCurrent=current;
current=current->next;
}
return that;
}
template <class T>
ostream &operator<<(ostream &outStream,linkedList<T> list)
{
T element;
this.current=this.head;
while(this.current->!=NULL)
{
element=this.retrieve(element);
outStream<<"["<<element<<"]"<<endl;
this.current++;
}
return outStream;
}
#endif
.cpp有很多节点,我似乎找不到这个seg错误的来源。
linkedListApp.cpp
#include "linkedList.h"
#include "myDate.h"
#include <iostream>
using namespace std;
#include <fstream>
//FillList
//Description: opens a file and fills the list from the file
//Parameters: indexList
//Return: none
void FillList(linkedList<myDate> &list);
//DisplayList
//Description: displays the list to the monitor
//Parameters: indexList
//Return: none
void DisplayList(linkedList<myDate> list);
//AddToList
//Description: adds to the list by ordered insert
//Parameters: indexList,num
//Return: none
void AddToList(linkedList<myDate> &list);
//GetTodaysAppointments
//Description: Asks the user for today's date, and looks for a matching date from the list
//Parameters: indexList,num
//Return: none
void GetTodaysAppointments(linkedList<myDate> list);
//ChangeAppointment
//Description: Lets the user choose an appointment to change, and changes as such
//Parameters: indexList
//Return: None
void ChangeAppointment(linkedList<myDate> &list);
//Menu
int menu();
int main()
{
int choice;
int num;
linkedList<myDate> L;
while ((choice = menu()) != 6)
{
switch (choice)
{
case 1: FillList(L); break;
case 2: AddToList(L); break;
case 3: GetTodaysAppointments(L); break;
case 4: ChangeAppointment(L); break;
case 5: DisplayList(L); break;
}
}
return 0;
}
//menu
int menu()
{
int ch;
cout << endl;
cout << "1. Fill from file" << endl;
cout << "2. Add to the list" << endl;
cout << "3. Get Todays Appointments" << endl;
cout << "4. Change an appointment" << endl;
cout << "5. Display list" << endl;
cout << "6. Quit"<<endl;
cout << "Choice: ";
cin >> ch;
return ch;
}
//FillList
//Description: opens a file and fills the list from the file
//Parameters: indexList
//Return: none
void FillList(linkedList<myDate> &list)
{
myDate aDate;
myTime aTime;
ifstream f;
bool result =true;
string fname;
cout << "File: ";
cin >> fname;
f.open(fname.c_str());
if (f.fail())
{
cout << "Failed to open" << endl;
return;
}
list.begin();
f >> aDate;
while (result && !f.eof())
{
f>>aTime;
aDate.setTime(aTime);
list.orderedInsert(aDate);
f>>aDate;
}
f.close();
}
每当我试图"填充"时,我都会得到一个seg错误,我的其余代码都在这里,因为我有更多的文件。但我想你可以从这两个中找到seg的错误,我就是不能
这只是对代码的一些想法-它不是决定性的,而且可能不是segfault的解决方案。
你似乎误解了如何使用指针。例如,在析构函数中,您为temp
分配了内存,然后立即将其设置为head。这基本上是一次泄漏。您可以简单地将其设置为head
。除非你打算使用它,否则没有理由为它分配内存。你在整个代码中都会遇到这个问题。我会先学习指针是如何工作的,因为这对许多其他事情都是至关重要的。
template<class T>
linkedList<T>::~linkedList()
{
node *temp=new node;
while(head->!=NULL)
{
temp=head;
head=head->next;
delete temp;
}
}
搜索功能中的一个潜在问题:
while(current->data!=value||current->!=NULL)
你的想法是对的,但点错了。如果current
为空怎么办?看看短路评估。
您多次使用current
似乎是一个糟糕的设计,但可能有一些需求我不知道。
我注意到了其他一些事情,但如果你需要审查你的代码,还有其他地方可以问。