Troubles with C++ list class template!
May 7, 2009 at 1:04am UTC
...
Last edited on May 7, 2009 at 3:18am UTC
May 7, 2009 at 1:42am UTC
Templates must be implemented in the header. You cannot have a separate implementation file.
May 7, 2009 at 2:05am UTC
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??
May 7, 2009 at 2:25am UTC
find() is returning NULL, which suggests that index is invalid.
May 7, 2009 at 3:18am UTC
Figured out my problem.
Thanks guys! =D
Last edited on May 7, 2009 at 3:18am UTC
Topic archived. No new replies allowed.