Ive been having some trouble recently with a copy constructor (related to an assigment operator which is overloaded).... ive been getting segmentation fault, and Im pretty sure that i've narrowed it down to the copy constructor
Anyway, here is my code for the copy constructor:
template <typename L>
List<L>::List(const List<L>& another)
{
first = NULL;
last= NULL;
length = 0;
Node<L>* temp = another.first;
temp->next=another.first->next;
int count=0;
while (count < another.size())
{
pushBack(temp->data);
temp=temp->next;
count++;
}
}
it could also be happening in my operator overloading, and ive been having some trouble with that, but i dont think it is, either way here it is:
template <typename L>
const List<L> List<L>::operator=(const List<L>& another)
{
Node<L>* tempLeft = last;
tempLeft->previous = last->previous;
Node<L>* tempRight = another.first;
tempRight->next=another.first->next;
int count = 0;
if (size() <= another.size())
{
while (count < size())
{
delete tempLeft;
tempLeft = tempLeft->previous;
pushBack (tempRight->data);
tempRight = tempRight->next;
count++;
}
while (count < another.size())
{
pushBack (tempRight->data);
tempRight = tempRight->next;
count++;
}
}
else
{
while (count < another.size())
{
delete tempLeft;
tempLeft = tempLeft->previous;
pushBack (tempRight->data);
tempRight = tempRight->next;
count++;
}
while (count < size())
{
delete tempLeft;
tempLeft = tempLeft->previous;
count++;
}
}
return *this;
}
Here is an example of the main function (:
int main()
{
cout<<"***Working With A List Of Strings ***"<<endl;
cout << "it wont even print this";
List<string> students;
List<string>::Iterator<string> pos;
any advice will be welcomed, thanks!