Problem with templates
I am trying to write a template class for a simple double linked list. Posted below are the definitions. In the .cpp file I have the implementation. I know for certain that I am including the .h and .cpp files in the project, but I get an error when I try to build:error LNK2019: unresolved external symbol "public: __thiscall wcLinkedList<int>::~wcLinkedList<int>(void)" (?1?$wcLinkedList@H@@QAE@XZ) referenced in function _main
From mymain()here is the code that is causing the problem:
void
main(int argc,char **argv) {
wcLinkedList<int> test;
getchar();
}
Where am I going wrong with these templates?
Thanks,
Graham/*********************************************************************************************/
template
<class T>class
wcLLNode {private
:T value;
wcLLNode *next, *prev;
public
:wcLLNode();
wcLLNode(T value, wcLLNode *next, wcLLNode *prev);
~wcLLNode() {}
};
/*********************************************************************************************/
template
<class T>class
wcLinkedList {private
:wcLLNode<T> *head;
//Points to the head of the listwcLLNode<T> *tail;
//Points to the end of the listwcLLNode<T> *cur;
//Index into the listint count;//Number of nodes in the listpublic
:wcLinkedList();
//Default constructor~wcLinkedList();
//Default destructorvoid Add(T value);//Add a node to the listint Remove(void);int Remove(T value);//Remove a node from the listT Next(
void);//Move to the next node in the list and return its ptrT Previous(
void);//Move to the previous node in the list and return its ptrT First(
void);//Move to the first node and return its ptrT Last(
void);//Move to the last node and return its ptrinlineint Count(void) {returnthis->count; }//Return the number of nodes in the listinlinebool AtHead(void) {returnthis->cur ==this->head; }//Check to see if we are at last nodeinlinebool AtTail(void) {returnthis->cur ==this->tail; }//Check to see if iterator is at last node};
[5988 byte] By [
GrahamH] at [2007-12-16]
I did something like the following and it worked with no linker errors:
My compilation command is: cl /EHsc main.cpp.
I am using VC2005. Also, I have modified the destructor code to avoid some compile time errors.
main.cpp included temp.h where the template is declared.
//main.cpp
#include "temp.h"
#include <stdio.h>
#include <iostream>
template<class T>
wcLLNode<T>::wcLLNode() {
this->value = NULL;
this->next = NULL;
this->prev = NULL;
}
template<class T>
wcLLNode<T>::wcLLNode(T value, wcLLNode *next, wcLLNode *prev) {
this->value = value;
this->next = next;
this->prev = prev;
}
/*****************************************************************************************/
//Default constructor
template<class T>
wcLinkedList<T>::wcLinkedList() {
this->count = 0;
this->head = NULL;
this->tail = NULL;
this->cur = NULL;
}
//Default destructor
template<class T>
wcLinkedList<T>::~wcLinkedList() {
/*wcLLNode *tmpPtr = this->head;
while (tmpPtr != NULL) {
this->cur = tmpPtr->next;
delete tmpPtr;
tmpPtr = this->cur;
this->count--;
}*/
}
//Add a node to the list
template<class T>
void wcLinkedList<T>::Add(T value) {
wcLLNode *tmpNode;
if (this->count == 0) {
tmpNode = new wcLLNode(value, NULL, NULL);
this->head = tmpNode;
}
else {
= new wcLLNode(value, NULL, this->tail);
this->tail->next = tmpNode;
}
this->cur = tmpNode;
this->tail = tmpNode;
}
//Remove last node from the list
template <class T>
int wcLinkedList<T>::Remove(void) {
switch (this->count) {
case 0: return -1;
break;
case 1:
this->cur = NULL;
this->tail = NULL;
delete this->head;
break;
default:
this->cur = this->tail->prev;
delete this->tail;
this->tail = this->cur;
break;
}
this->count--;
return this->count;
}
//Remove a specific node from the list
template <class T>
int wcLinkedList<T>::Remove(T value) {
return -1;
}
//Move to the next node in the list and return its ptr
template <class T>
T wcLinkedList<T>::Next(void) {
if (this->cur == this->tail) return NULL;
this->cur = this->cur->next;
return this->cur->value;
}
//Move to the previous node in the list and return its ptr
template <class T>
T wcLinkedList<T>::Previous(void) {
if )this->cur == this->head) return NULL;
this->cur = this->cur->prev;
return this->cur->value;
}
//Move to the first node and return its ptr
template <class T>
T wcLinkedList<T>::First(void) {
this->cur = this->head;
return this->cur->value;
}
//Move to the last node and return its ptr
template <class T>
T wcLinkedList<T>::Last(void) {
this->cur = this->tail;
return this->cur->value;
}
void main(int argc, char **argv) {
wcLinkedList<int> test;
getchar();
}
//temp.h
/*********************************************************************************************/
template<class T>
class wcLLNode {
private:
T value;
wcLLNode *next, *prev;
public:
wcLLNode();
wcLLNode(T value, wcLLNode *next, wcLLNode *prev);
~wcLLNode() {}
};
/*********************************************************************************************/
template<class T>
class wcLinkedList {
private:
wcLLNode<T> *head; //Points to the head of the list
wcLLNode<T> *tail; //Points to the end of the list
wcLLNode<T> *cur; //Index into the list
int count; //Number of nodes in the list
public:
wcLinkedList(); //Default constructor
~wcLinkedList(); //Default destructor
void Add(T value); //Add a node to the list
int Remove(void);
int Remove(T value); //Remove a node from the list
T Next(void); //Move to the next node in the list and return its ptr
T Previous(void); //Move to the previous node in the list and return its ptr
T First(void); //Move to the first node and return its ptr
T Last(void); //Move to the last node and return its ptr
inline int Count(void) { return this->count; } //Return the number of nodes in the list
inline bool AtHead(void) { return this->cur == this->head; } //Check to see if we are at last node
inline bool AtTail(void) { return this->cur == this->tail; } //Check to see if iterator is at last node
};
Thanks,
Ayman Shoukry
VC++ Team