I AM NOT ALLOWED TO USE STL CONTAINERS!
I am getting a Segmentation fault from the SortedListClass(const SortedListClass< T > &rhs)
, operator=(const SortedListClass< T > &rhs)
, and clear()
functions in the following code.
This is the result from the debugger:
#0 __GI___libc_free (mem=0x4) at malloc.c:2929 #1 0x00000000004016d6 in SortedListClass::clear (this=0x7ffffffde470) at SortedListClass.inl:45 #2 0x00000000004011b5 in SortedListClass::operator= (this=0x7ffffffde470, rhs=...) at SortedListClass.inl:28 #3 0x0000000000400c88 in main () at project5.cpp:33
SortedListClass.inl
line 45 is the following: delete tempHead;
SortedListClass.inl
line 28 is the following: this->clear();
project5.cpp
line 33 is the following: copyTestList = testList;
Here is the code:
SortedListClass.inl
template<class T>
SortedListClass< T >::SortedListClass()
{
head = NULL;
tail = NULL;
cout<<"Empty list intialized...."<<endl;
}
template<class T>
SortedListClass< T >::SortedListClass(const SortedListClass< T > &rhs)
{
cout<<"Copied Value(s)"<<endl;
LinkedNodeClass< T > *rhsFront;
LinkedNodeClass< T > *newCopyNode;
rhsFront = rhs.head;
while(rhsFront != NULL)
{
newCopyNode = new LinkedNodeClass< T >(rhsFront,rhsFront->getValue(),
rhsFront->getNext());
rhsFront = newCopyNode;
rhsFront = rhsFront->getNext();
}
}
template<class T>
void SortedListClass< T >::operator=(const SortedListClass< T > &rhs)
{
this->clear();
*this = SortedListClass(rhs);
}
template<class T>
void SortedListClass< T >::clear()
{
LinkedNodeClass< T >*tempHead;
LinkedNodeClass< T >*tempTail;
tempHead = head;
tempTail = tail;
while(tempHead != NULL || tempTail != NULL)
{
cout << "Deleting node(s) --> "<< endl;
tempHead = head;
delete tempHead;
tempHead = NULL;
head = tempHead;
tempTail = tail;
delete tempTail;
tempTail = NULL;
tail = tempTail;
}
cout<<"Deleting complete"<<endl;
}
project5.cpp
int main()
{
SortedListClass< int >testList;
testList.insertValue(3);
testList.printForward();
testList.insertValue(4);
testList.printForward();
int theVal = 0;
SortedListClass<int>copyTestList(testList);
copyTestList = testList;
copyTestList.getNumElems();
int index = 0;
int outval = 0;
copyTestList.getElemAtIndex(index,outval);
copyTestList.printForward();
copyTestList.removeLast(theVal);
copyTestList.printBackward();
return 0 ;
}