Hi, homework problem here. This should be simple to fix but I can't seem to figure it out. The problem lies in the copy constructor, I'll post the code I wrote for it and the functions called from it.
//relevant functions from SkipList.inl
//copy constructor
template <typename T>
SkipList<T>::SkipList(const SkipList<T> & orig)
{
init();
clone(orig);
}
//clone segment reused in copy constructor and operator=
template <typename T>
void SkipList<T>::clone(const SkipList<T> & orig)
{
Node<T> * temp = new Node<T>;
temp = orig.begin;
current = begin;
while( current != 0 )
{
current->next = temp->next;
current->prev = temp->prev;
current->data = temp->data;
temp = temp->next;
current = current->next;
}
this_size = orig.this_size; //this causes the segfault (?)
}
//set up a new list
template <typename T>
void SkipList<T>::init()
{
this_size = 0;
begin = new Node<T>;
end = new Node<T>;
begin->next = end;
begin->prev = 0;
end->next = 0;
end->prev = begin;
}
I've figured out that copying the 'this_size' variable (just an integer holding the size of the list) causes the segfault. I don't really understand why, and if I comment out that line it works fine and I can perform operations on the copied list just fine. I wouldn't think that copying over an integer, which uses no memory allocation, would cause a segfault...