Hello all, I am having some trouble creating and using a template based linked list and I hope you all can help me out. I have created a LinkedList class, and I am trying to use it in my main, but I get a unresolved external link error, that I hope you all can help me with.


#pragma once
#include <iostream>

using namespace std;

template < class T >
class LinkedList{ 


class ListNode{ 


T value; // Node value
ListNode *next; // Next Node
ListNode ( T v, ListNode *n = NULL) : value( v ), next( n ) { }
}; // ListNode

ListNode *head;


LinkedList ( ListNode *ptr = NULL ) { head = ptr; }
~LinkedList ( void );
void appendNode ( T object);
void insertNode ( T object);
void deleteNode ( T object);
void displayList ( void );
}; // LinkedList


#include "LinkedList.h"

template < class T >
void LinkedList<T>::appendNode( T object ){ 
ListNode *newNode = new ListNode( object ), *nodePtr;
if ( !head )
head = newNode;
else { 
for ( nodePtr = head; nodePtr->next; nodePtr = nodePtr->next )
nodePtr->next = newNode;

I get the error in my main when I use the -> append, as so:

#include <iostream>
#include "LinkedList.h"

using namespace std;

int main(){

LinkedList<double> *myList = new LinkedList<double>;
myList->appendNode(45.67); //this is what gives me the error...

	return 0;

Can you help me out? Thanks!

10 Years
Discussion Span
Last Post by bugmenot

I tried your code and it didn;t work, as you said. then I tried putting the implementation inside the class and is did work i.e.

// header file
template<class T>
class LinkedList{
void AddNode(){
   // addnode code here...

Check out the following site, it states that class template implementations must be in the header file - I guess this means that an additional copy of the compiled member functions is produced for each file using the class... but I dunno.


it says the following:

There is never an implementation file (*.cpp) for a template class. All of the member functions should be declared in the header file (in this case, table.h). The following shows how the functions for this class would be declared after the declaration of the class.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.