Groups | Blog | Home
all groups > visual c > october 2004 >

visual c : templates, C++, and LNK2019


Chris Burrger via .NET 247
10/31/2004 3:54:57 AM
I've found some other posts that deal with this linker error, but none of those solutions work for me. I just got VS.NET 2003 and I wanted to try it our with some basic algorithms. I am trying to make a singly linked list, using templates.
I?ll post my code and then the error message at the end.
//////////Node.h
#ifndef __CSL_NODE
#define __CSL_NODE
namespace CSL {
template<typename T> class Node {
public:
Node( T nm );
Node( T nm, Node* n );
~Node();
public:
T name; Node* next;
};
}
#endif

/////////Node.cpp
#include "Node.h"
namespace CSL {
template<typename T>
Node<T>::Node( T nm ) {
name = nm; next = 0;
}
template<typename T>
Node<T>::Node( T nm, Node* n ) {
name = nm; next = n;
}
template<typename T>
Node<T>::~Node() {
}
}

///////SingleLinkedList.h
#ifndef __CSL_SINGLELINKEDLIST
#define __CSL_SINGLELINKEDLIST
#include "Node.h"
namespace CSL {
template<typename T>
class SingleLinkedList {
public:
SingleLinkedList();
~SingleLinkedList();
public:
void Add( T name );
void Remove( T name );
void Reverse();
void PrintOut();
private:
int length; Node<T> *root; Node<T> *rear;
};
}
#endif

////////SingleLinkedList.cpp
#include <string.h>
#include <stdio.h>
#include "Node.h"
#include "SingleLinkedList.h"
namespace CSL {
template<typename T>
SingleLinkedList<T>::SingleLinkedList(): length(0) {
root = 0; rear = 0;
}
template<typename T>
SingleLinkedList<T>::~SingleLinkedList() {
//for a shorter post
}
template<typename T>
void SingleLinkedList<T>::Add( T name ) {
//for a shorter post
}
template<typename T>
void SingleLinkedList<T>::Remove( T name ) {
//for a shorter post
}
template<typename T>
void SingleLinkedList<T>::Reverse() {
//for a shorter post
}
template<typename T>
void SingleLinkedList<T>::PrintOut() {
//for a shorter post
}
}

//// LibTest.cpp
#include "SingleLinkedList.h"
#include "Node.h"
using namespace CSL;
int main(int argc, char* argv[]) {
SingleLinkedList<int> sll; //LNK2019 error
sll.Add( 1 );
sll.Add( 10 );
sll.Add( 5 );
sll.PrintOut();
//now rev
sll.Reverse();
sll.PrintOut();
//now add
sll.Add( 200 );
sll.Reverse();
sll.PrintOut();
return 0;
}

I get 5 LNK2019 errors when I try to compile, here is the first one:
LibTest error LNK2019: unresolved external symbol "public: __thiscall CSL::SingleLinkedList<int>::~SingleLinkedList<int>(void)" (??1?$SingleLinkedList@H@CSL@@QAE@XZ) referenced in function _main


Any help would be appreciated.
Thanks!


--------------------------------
From: Chris Burrger

-----------------------
Posted by a user from .NET 247 (http://www.dotnet247.com/)

Bo Persson
10/31/2004 1:37:19 PM

"Chris Burrger via .NET 247" <anonymous@dotnet247.com> skrev i
meddelandet news:OY8gyB0vEHA.1300@TK2MSFTNGP14.phx.gbl...
[quoted text, click to view]

To have you template separated between the .h and the .cpp files, you
would have to use the 'export' keyword.

export template<class T>
struct etc.


Unfortunately, this isn't supported by the MS compiler (or most other
compilers for that matter). Until it is, you have to include the
definitions in the header file.


Bo Persson

Vladimir Nesterovsky
10/31/2004 2:10:56 PM
[quoted text, click to view]
those solutions work for me. I just got VS.NET 2003 and I wanted to try it
our with some basic algorithms. I am trying to make a singly linked list,
using templates.

You can add #include of cpp files in the end of the h files for those
headers that define templates.

//////////Node.h
#ifndef __CSL_NODE
#define __CSL_NODE

....

#includer "Node.cpp"
#endif
--
Vladimir Nesterovsky
e-mail: vladimir@nesterovsky-bros.com
home: www.nesterovsky-bros.com

AddThis Social Bookmark Button