"LNK2019 - Unresolved external Symbol" while using templates

M

Mastadex

So here is my Code:

template <class T>
struct Nodes
{
T *Data; // The Image
char Name[50]; // The Name of the image

Nodes<T> *Prev;
Nodes<T> *Next;
};
typedef struct Nodes Node;


template <class T>
class List
{
Node *Current;

public:

List();
~List();

};

---------------------------------------------------

#include "List.h"

template <class T>
List<T>::List()
{

}

template <class T>
List<T>::~List()
{

}

====================================================

error LNK2019: unresolved external symbol "public: __thiscall
List<class Image>::~List<class Image>(void)"
(??1?$List@VImage@@@@QAE@XZ) referenced in function main...

Thats the error I get. Is this one of those subtle c++ bugs that I need
to work around? my main is quite simple, all it does is instanciate the
class. thats all:

List<int> foo;

Im completely confused.
 
M

mlimber

Mastadex said:
So here is my Code:

template <class T>
struct Nodes
{
T *Data; // The Image
char Name[50]; // The Name of the image

Nodes<T> *Prev;
Nodes<T> *Next;

There's no need for the said:
};
typedef struct Nodes Node;

This is unnecessary in C++. You can drop this line and not change any
other code.
template <class T>
class List
{
Node *Current;

public:

List();
~List();

};

---------------------------------------------------

#include "List.h"

template <class T>
List<T>::List()
{

}

template <class T>
List<T>::~List()
{

}

====================================================

error LNK2019: unresolved external symbol "public: __thiscall
List<class Image>::~List<class Image>(void)"
(??1?$List@VImage@@@@QAE@XZ) referenced in function main...

Thats the error I get. Is this one of those subtle c++ bugs that I need
to work around? my main is quite simple, all it does is instanciate the
class. thats all:

List<int> foo;

Im completely confused.

You need to put the template function definitions in the header file.
See this FAQ:

http://www.parashift.com/c++-faq-lite/templates.html#faq-35.7

Also, unless you're doing homework here, I'd suggest using std::list
instead of reinventing the wheel.

Cheers! --M
 
J

John Fullman

mlimber's got it exactly.
You need to put all templates in the header file.
There should be no .cpp file for templated classes.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top