Linked List Copy Constructor

I have been trying to figure out how to write a copy constructor, but whenever I run it it doesn't compile. Logically, I feel like what I'm doing makes sense, but apparently my compiler doesn't seem to think so.

This is my AddTail function, which is used in my copy constructor.

template<typename T>
void LinkedList<T>::AddTail(const T& data)
{
if (tail == nullptr)
{
tail = new Node;
tail->data = data;
head = tail;
numNodes++;
}
else
{
Node* temp = new Node;
temp->data = data;
temp->prev = tail;
tail->next = temp;
tail = tail->next;
numNodes++;
}
}


[template<typename T>
LinkedList<T>::LinkedList(const LinkedList<T>& list)
{
if (list.head == nullptr)
{
return;
}

Node* currentNode = list.head;

for (unsigned int i = 0; i < list.numNodes; i++)
{
AddTail(currentNode->data);
currentNode = currentNode->next;
}
}]

I've also tried using a while loop instead of the for loop, but that doesn't seem to make much of a difference. My addTail function seems to work, so I don't think the issue is there.
If all your compiler says is "compiler error" then you need a new compiler.

And use code tags when posting code.
Last edited on
Topic archived. No new replies allowed.