So I have been coding in C++ for not so long and am struggling with a copy constructor and my program keeps on crashing when I use it.
Node:
template <class T>
class Node
{
public:
Node(T data, Node<T> * n = 0)
{ element = data;
next = n;
prev = n;}
~Node()
{ next = 0;
prev = 0;}
T element;
Node<T>* next;
Node<T>* prev;
Function Code:
CircularList<T>::CircularList(const CircularList<T> & other)
{
if(other.tail == NULL)
{
this->tail = NULL;
}
else
{
this->tail = other.tail;
Node<T> * original = this->tail;
Node<T> * temp = other.tail->next;
while(temp != other.tail)
{
original->next = temp;
temp = temp->next;
original = original->next;
}
original->next = temp;
}
}
(EDIT) Here is the specification for the CircularList Class. I think I just do not understand what steps to take to make it deep copy rather than shallow copy.
template<class T>
class CircularList;
template<class T>
ostream& operator<<(ostream&,CircularList<T>&);
template<class T>
class CircularList : public LinearStructure<T>
{
public:
friend ostream& operator<< <T>(ostream&,CircularList<T>&);
CircularList();
CircularList(const CircularList<T>& other);
CircularList<T>& operator=(const CircularList<T>& other);
virtual CircularList<T>* clone();
virtual ~CircularList();
virtual void insert(int index, T element);
virtual T remove(int index);
virtual T get(int index) const;
virtual bool isEmpty();
virtual void clear();
Node<T>* getLeader();
protected:
ostream& print(ostream& os);
private:
int size() const;
Node<T>* tail;
};
So yeah when I test it with this (the list isn't empty):
CircularList<int> clist;
clist.insert(0,8);
CircularList<int> clist2(clist);
It crashes. Any help would be appreciated!!