Troubles with C++ list class template!

...
Last edited on
Templates must be implemented in the header. You cannot have a separate implementation file.
Hmm, didn't know that, this is my first time dealing with template.
Okay, i combined the header and the implementation. It compiles, but the windows error pops up right after i executed it.
When i use debug, the arrow points at this line(the commented line)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
template <typename T>
void List<T>::insert(int index, const T & newItem)
{
	int newLength = getLength()+1;

	ListNode<T> *newPtr = new ListNode<T>;
	size = newLength;
	newPtr->item = newItem;

	if(index == 1)
	{
		newPtr->next = head;
		head = newPtr;
	}
	else
	{
		ListNode<T> *prev = find(index-1);
		//newPtr->next = prev->next;
		prev->next = newPtr;
	}
}

Can't really see any problem...
What's wrong with it??
find() is returning NULL, which suggests that index is invalid.
Figured out my problem.
Thanks guys! =D
Last edited on
Topic archived. No new replies allowed.