Ok having some issues here with a Template approach, i am not 100% sure what to do, it comiles fine however i get the following linking error. I am currently using visual stupio however i might give it a try in codeblocks on linux and see if it helps.
CODE
1>Linking...
1>hanoi.obj : error LNK2001: unresolved external symbol "public: __thiscall bbq::LinkedList<char>::LinkedList<char>(void)" (??0?$LinkedList@D@bbq@@QAE@XZ)
1>C:\Users\Admin\Documents\Visual Studio 2008\Projects\Assignment2\Release\Assignment2.exe : fatal error LNK1120: 1 unresolved externals
I am completely lost as to why and i have googled it and found no solution that i understand for it
I am very tired so if its something stupid i won't be to suprised at all
here is my codes
node.h
cpp
namespace bbq
{
template <class T>
class Node
{
//keep all variables public for simplicity
public :
typedef T value_type;
Node(); //constructor
value_type data; //data to be stored in each node
Node* next; //pointer to the next node in list.
};
}
node.cpp
cpp
#include <iostream>
#include "node.h"
namespace bbq
{
template <class T> Node<T>::Node()
{
}
}
linkedlist.h
cpp
// linkedlist.h defines a class LinkedList
// this class stores instances of class Node
// used for dynamic memory allocation in replacement
// of Arrays to store large volumes of repetitious data.
#ifndef LINKEDLIST_HEADER_H
#define LINKEDLIST_HEADER_H
namespace bbq
{
template <class T>
class LinkedList
{
public:
LinkedList();
// constructor, creates a pointer headNode
// initialised to NULL, current pointer points
// to headnode
void addFirstNode(T aValue);
// Function is used for the very first value only
// FirstNode next is initialised to null, headnode
// changes to point to firstNode of linkedlist
// auxiliary pointer current is now pointing to first
// node.
// Precondition : aValue is valid class type.
// PostCondition : None
void addNode(T aValue);
// Function use to add nodes to linkedlist beyond first node
// Similar to addFirstNode Function, Auxiliary current pointer
// is adjusted and the new nodes next pointer is initialised to
// NULL, the previous element of the list is adjusted to point
// to newly created instance of node.
// Precondition : aValue is valid class type.
// PostCondition : None
void printList();
// Function to print out all elements in linked list, will print
// until final node->next = NULL (indicating end of linkedlist).
// Will work correctly for primitive types stored in linkedlist.
// Modification needed to print objects.
// Precondition : LinkedList is valid - contains elements
// Postcondition : data printed is of primitive type
private:
Node<T>* headNode; // pointer to beginning of linkedlist
Node<T>* current; // pointer to current element in linked list.
};
}
#endif
linkedlist.cpp
cpp
#include <iostream>
#include "node.h"
#include "linkedlist.h"
namespace bbq
{
//constructor
template <class T>LinkedList<T>::LinkedList()
{
Node<T>* headNode; //node pointer
headNode = NULL; //initialisd to NULL
current = headNode; //current is initialised to point to headnode
}
//function for adding first node to a list
template <class T> void LinkedList<T>::addFirstNode(T aValue)
{
Node<T>* someNode;
someNode = new Node(); //instance of node created
someNode->data = aValue; //data is set
someNode->next = NULL; //the next node in series is NULL
//not created as of yet.
headNode = someNode; // Direct head-node to point to first element
current = someNode; // the current element is now newly created node
}
// Function for adding nodes to list - only to be used after addFirstNode has been called
template <class T> void LinkedList<T>::addNode(T aValue)
{
Node<T>* nextNode;
nextNode = new Node();
nextNode->data = aValue; // set data for new node
nextNode->next = NULL; // initialise next element in list to NULL
current->next = nextNode; // point the previous nodes next to newly created node.
current = nextNode; // move current to newly created node
}
// function to print contents of list - primitive types
template <class T> void LinkedList<T>::printList()
{
Node* start;
start = headNode; //create a node to traversing of list.
do
{
cout << start->data; //print data of each node
start = start->next;
} while (start->next != NULL); //once next == NULL
//end of list reached
cout<< start->data << endl << endl; // print final element of list
}
}
main.cpp
cpp
#include <iostream>
#include <stack>
#include <string>
#include "node.h"
#include "linkedlist.h"
using namespace std;
using namespace bbq;
//functions
void printIntroMessage();
int main()
{
printIntroMessage();
LinkedList<char>* myList = new LinkedList<char>();
/*
myList->addFirstNode('A');
myList->addNode('B');
myList->printList();*/
return 0;
}
void printIntroMessage()
{
//cout <<"This is not working
" << endl;
}
I am really lost, as to why i am getting the error, any help would be great... or a kick in the right direction
cheers
This post has been edited by bbq: 26 Sep, 2008 - 04:56 AM