I am having trouble coding a Linked List Template. It compiles but I get these errors:
1>main.obj : error LNK2019: unresolved external symbol "public: __thiscall LinkedList::~LinkedList(void)" (??1?$LinkedList@H@@QAE@XZ) referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol "public: void __thiscall LinkedList::deleteNode(int)" (?deleteNode@?$LinkedList@H@@QAEXH@Z) referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol "public: void __thiscall LinkedList::displayList(void)" (?displayList@?$LinkedList@H@@QAEXXZ) referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol "public: void __thiscall LinkedList::insertNode(int)" (?insertNode@?$LinkedList@H@@QAEXH@Z) referenced in function _main
1>c:\users\pr0hz\documents\visual studio 2010\Projects\ListTemplate\Debug\ListTemplate.exe : fatal error LNK1120: 4 unresolved externals
Any help would be greatly appreciate as I have been staring at my screen for the past couple of hours trying to solve this problem. Here is my code:
main.cpp
#include <iostream>
#include <conio.h>
#include "LinkedList.h"
using namespace std;
int main()
{
LinkedList<int> list;
list.insertNode(5);
list.insertNode(7);
list.insertNode(1);
list.insertNode(19);
list.insertNode(16);
list.insertNode(22);
cout << "\nDisplay the list: \n";
list.displayList();
cout << endl;
list.deleteNode(16);
cout << "\nDisplay node with 16 deleted: \n";
list.displayList();
cout << endl;
LinkedList<int> list2 = list;
cout << "\nDisplay List 2: \n";
list2.displayList();
cout << endl;
getch();
return 0;
}
LinkedList.h
#ifndef LINKEDLIST_H
#define LINKEDLIST_H
#include <iostream>
using namespace std;
template <class T>
class LinkedList
{
private:
struct ListTemplate
{
T value;
ListTemplate *next;
};
ListTemplate *head;
public:
//Constructor
LinkedList()
{ head = NULL; }
//Destructor
~LinkedList();
//Copy Constructor
LinkedList(ListTemplate &obj)
{
ListTemplate *nodePtr = &obj; //Move through the list
ListTemplate *newNode = new ListTemplate;
if(!head)
return;
else
{
newNode = head;
while(nodePtr->next)
{
nodePtr = nodePtr->next;
newNode->next = nodePtr;
}
}
}
void appendNode(T);
void insertNode(T);
void deleteNode(T);
void displayList();
};
#endif
LinkedList.cpp
#include "LinkedList.h"
#include <iostream>
using namespace std;
template <class T>
void LinkedList<T>::appendNode(T newValue)
{
ListTemplate *newNode; //Point to the new node
ListTemplate *nodePtr; //Move through the list
//Allocate new node and store num there
newNode = new ListTemplate;
newNode->value = num;
newNode->next - NULL;
if(!head)
head = newNode;
else
{
//Initialize the nodePtr as head to move through the list
nodePtr = head;
//While there is still a node, we move through it until there is no more
while (nodePtr->next)
nodePtr = nodePtr->next;
//Append the node
nodePtr->next = newNode;
}
}
template <class T>
void LinkedList<T>::insertNode(T newValue)
{
ListTemplate *newNode; //New Node
ListTemplate *nodePtr; //Move through the list
ListTemplate *previousNode = NULL; //The previous node
//Allocate new node and store num
newNode = new ListTemplate;
newNode->value = num;
//If no node, new node is head
if (!head)
{
head = newNode;
newNode->next = NULL;
}
else //Insert node where it needs to be
{
nodePtr = head; //Beginning position
previousNode = NULL;
//Skip nodes that are less than newNode
while(nodePtr != NULL && nodePtr->value < num)
{
previousNode = nodePtr;
nodePtr = nodePtr->next;
}
//If newNode is the 1st in the list, insert it in slot 1
if (previousNode == NULL)
{
head = newNode;
newNode->next = nodePtr;
}
else //Insert after previous node
{
previousNode->next = newNode;
newNode->next = nodePtr;
}
}
}
template <class T>
void LinkedList<T>::deleteNode(T searchNum)
{
ListTemplate *nodePtr;
ListTemplate *previousNode;
//If the list is empty, do nothing
if(!head)
return;
//See if the first node is the one
if(head->value == num)
{
nodePtr = head->next;
delete head;
head = nodePtr;
}
else //Find the node to delete
{
nodePtr = head;
//Skip the nodes not equal to num
while (nodePtr != NULL && nodePtr->value != num)
{
previousNode = nodePtr;
nodePtr = nodePtr->next;
}
//Delete node and link previous node to the one of the one being deleted
if (nodePtr)
{
previousNode->next = nodePtr->next;
delete nodePtr;
}
}
}
template <class T>
LinkedList<T>::~LinkedList()
{
ListTemplate *nodePtr;
ListTemplate *nextNode;
nodePtr = head;
while (nodePtr != NULL)
{
nextNode = nodePtr->next;
delete nodePtr;
nodePtr = nextNode;
}
}
template <class T>
void LinkedList<T>::displayList()
{
ListTemplate *nodePtr;
nodePtr = head;
while(nodePtr)
{
cout << nodePtr->value << " ";
nodePtr = nodePtr->next;
}
}
Once again, thanks for your help!