Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

LinkedList Template Trouble

Status
Not open for further replies.

artfuljames

Programmer
Nov 25, 2003
4
GB
I am having some linking errors with my LinkedList. Here is my LinkedList.h file:

template <class T>
class LinkedList{
private:
class Node{
private:
T value; // holds the elements in template array of MAX size
Node* pNext; // holds next item pointer
public:
Node(T value, Node* pNext); // constructor
T getValue(); // constructor that takes an element as a parameter
Node* getNext();
//add(const T&); // adds an element to the array returns true if successful, false if not
};
Node* theList;

public:
LinkedList();
void add(const T&);
void display();
/*

int find(const T&); // finds an element, returns position in the array
void displayElement(int); // displays found element, takes position as a parameter

bool remove(int); // removes element, taking position in array as parameter
void display(); // displays all elements in the array
int getSize(); // gets size of array collection
*/
};

template <class T>
LinkedList<T>::Node::Node(T value, Node* pNext){
this->value = value;
this->pNext = pNext;
}

template <class T>
T LinkedList<T>::Node::getValue(){
return value;
}

template <class T>
LinkedList<T>::Node* LinkedList<T>::Node::getNext(){
return pNext;
}

template <class T>
LinkedList<T>::LinkedList(){
theList = 0;
}

template <class T>
void LinkedList<T>::add(const T& value){
Node* nodeToAdd = new Node(value, theList);
theList = nodeToAdd;
}

template <class T>
void LinkedList<T>::display(){
Node* listPtr = theList;
if(listPtr != 0){
do{
cout << listPtr->getValue() << endl;
listPtr = listPtr->getNext();
}while(listPtr != 0);
}
}



I get the following errors when I run it with this test harness:

#include &quot;LinkedList.h&quot;
#include<iostream>
using namespace std;

int main(){
LinkedList<int> intArray;
// Store some int values...


intArray.add(1);
intArray.add(2);
intArray.add(3);
intArray.add(4);

cout << &quot;Contents are: \n&quot; << endl;
intArray.display();

return 0;

}



ERRORS:

studentDetailsTestLinkedList.obj : error LNK2001: unresolved external symbol &quot;public: __thiscall LinkedList<int>::Node::Node(int,class LinkedList<int>::Node *)&quot; (??0Node@?$LinkedList@H@@QAE@HPAV01@@Z)
studentDetailsTestLinkedList.obj : error LNK2001: unresolved external symbol &quot;public: class LinkedList<int>::Node * __thiscall LinkedList<int>::Node::getNext(void)&quot; (?getNext@Node@?$LinkedList@H@@QAEPAV12@XZ)
studentDetailsTestLinkedList.obj : error LNK2001: unresolved external symbol &quot;public: int __thiscall LinkedList<int>::Node::getValue(void)&quot; (?getValue@Node@?$LinkedList@H@@QAEHXZ)
Debug/assv2.exe : fatal error LNK1120: 3 unresolved externals
Error executing link.exe.


Please help
 
Its ok, i sorted it : )

when nesting classes, the implementation has to be performed directly underneath the function definition.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top