Reputation: 1
I have a program that compiles perfectly fine, but when I try and run a function through my menu, I am just responded with segmentation fault. I can't quite seem to find where the seg fault is happening either.
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
There are a lot of nodes is .cpp and I can't seem to find where this seg fault is coming from.
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();
}
Whenever I try to "fill" i just end up getting a seg fault, the rest of my code is here because I have more files. But I think you can find the seg fault from these two I just cant
Upvotes: 0
Views: 164
Reputation: 7706
This is just some thoughts on the code - it is not conclusive and it's probably not the solution for the segfault.
You seem to misunderstand how to use pointers. For example in your destructor, you allocated memory for temp
then immediately set it to head. This is basically a leak. You can simply set it to head
. There's no reason to allocate memory for it unless you plan on using it. You have this problem throughout your code. I would try to learn how pointers work first as that'll be fundamental to many other things.
template<class T>
linkedList<T>::~linkedList()
{
node *temp=new node;
while(head->!=NULL)
{
temp=head;
head=head->next;
delete temp;
}
}
A potential problem in your search function:
while(current->data!=value||current->!=NULL)
You have the right idea here but the ordering is wrong. What if current
is null? Take a look at short-circuit evaluation.
Your usage of current
seems like a bad design for the many times that you use it but perhaps there's some requirement I don't know about.
There are a couple other things I noticed but if you need review of your code, there's other places to ask that.
Upvotes: 1