artfuljames
Programmer
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 "LinkedList.h"
#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 << "Contents are: \n" << endl;
intArray.display();
return 0;
}
ERRORS:
studentDetailsTestLinkedList.obj : error LNK2001: unresolved external symbol "public: __thiscall LinkedList<int>::Node::Node(int,class LinkedList<int>::Node *)" (??0Node@?$LinkedList@H@@QAE@HPAV01@@Z)
studentDetailsTestLinkedList.obj : error LNK2001: unresolved external symbol "public: class LinkedList<int>::Node * __thiscall LinkedList<int>::Node::getNext(void)" (?getNext@Node@?$LinkedList@H@@QAEPAV12@XZ)
studentDetailsTestLinkedList.obj : error LNK2001: unresolved external symbol "public: int __thiscall LinkedList<int>::Node::getValue(void)" (?getValue@Node@?$LinkedList@H@@QAEHXZ)
Debug/assv2.exe : fatal error LNK1120: 3 unresolved externals
Error executing link.exe.
Please help
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 "LinkedList.h"
#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 << "Contents are: \n" << endl;
intArray.display();
return 0;
}
ERRORS:
studentDetailsTestLinkedList.obj : error LNK2001: unresolved external symbol "public: __thiscall LinkedList<int>::Node::Node(int,class LinkedList<int>::Node *)" (??0Node@?$LinkedList@H@@QAE@HPAV01@@Z)
studentDetailsTestLinkedList.obj : error LNK2001: unresolved external symbol "public: class LinkedList<int>::Node * __thiscall LinkedList<int>::Node::getNext(void)" (?getNext@Node@?$LinkedList@H@@QAEPAV12@XZ)
studentDetailsTestLinkedList.obj : error LNK2001: unresolved external symbol "public: int __thiscall LinkedList<int>::Node::getValue(void)" (?getValue@Node@?$LinkedList@H@@QAEHXZ)
Debug/assv2.exe : fatal error LNK1120: 3 unresolved externals
Error executing link.exe.
Please help