Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,577 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,893 people online right now. Registration is fast and FREE... Join Now!




linked list - template classes

 
Reply to this topicStart new topic

linked list - template classes, unresolved external errors

bbq
26 Sep, 2008 - 04:54 AM
Post #1

D.I.C Head
Group Icon

Joined: 15 May, 2008
Posts: 192



Thanked: 17 times
Dream Kudos: 50
My Contributions
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 crazy.gif

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 sad.gif" << 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 pirate.gif

This post has been edited by bbq: 26 Sep, 2008 - 04:56 AM
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Linked List - Template Classes
27 Sep, 2008 - 12:06 AM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,213



Thanked: 217 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
When dealing with template classes, they do not have a separate cpp implementation file. So take all the definitions in the linkedlist.cpp and move them into the header file linkedlist.h. Then delete the .cpp from the project.

Your linkedlist.h should now look like this...

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.

};

//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
}


}


#endif


Since you had the functionality in the .cpp file of a template class, it was looking to .h for the functionality. This is because template classes must define the functionality in the same file. Since it couldn't find it in the same file, it said it was undefined.

Hope that makes sense. Enjoy!

"At DIC we be template class header file merging code ninjas... we also merged a baby dolphin with capty's first born son... lets just say that kid could swim but he was pretty ugly!" decap.gif
User is offlineProfile CardPM
+Quote Post

bbq
RE: Linked List - Template Classes
27 Sep, 2008 - 02:50 AM
Post #3

D.I.C Head
Group Icon

Joined: 15 May, 2008
Posts: 192



Thanked: 17 times
Dream Kudos: 50
My Contributions
Ahhh awesome mate, I am so glad you took the time to help me out on that, i tried something similar after reading something, however it was wrong lol, Excellent biggrin.gif
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/3/08 12:17AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month