# Unresolved External Symbol Issue (Help)

This topic is 4727 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I had all my code in a single header file, and that compiled fine, but once I seperated the declaration from the implemetation things broke. I'm hoping someone here can tell me why, and how to fix it. I get these errors: Test.obj : error LNK2001: unresolved external symbol "public: __thiscall SL_List<int>::~SL_List<int>(void)" (??1?$SL_List@H@@QAE@XZ) Test.obj : error LNK2001: unresolved external symbol "public: __thiscall SL_List<int>::SL_List<int>(void)" (??0?$SL_List@H@@QAE@XZ) and my code is as follows: [SL_List.h]
#ifndef SL_LIST_H
#define SL_LIST_H

template<class Type>
struct Node
{//Node
Node<Type> *pNext;       // Pointer to the next Node
Type Data;               // The data field
};//Node

template<class Type>
class SL_List
{//SL_List
public:
SL_List(void);          //Empty Constructor
SL_List(const Type);    //Construct w/ Data
~SL_List(void);         //Destructor
int Insert(const Type); //Insert a new element
int Remove(void);       //Remove an existing element
private:
Node<Type> *pTail;      //End of the Linked List
};//SL_List

#endif


[SL_List.cpp]
#include "SL_List.h"

template<class Type>
SL_List<Type>::SL_List(void)
{
}

template<class Type>
SL_List<Type>::SL_List(const Type)
{
}

template<class Type>
SL_List<Type>::~SL_List(void)
{
}

template<class Type>
int SL_List<Type>::Insert(const Type)
{
}

template<class Type>
int SL_List<Type>::Remove(void)
{
}


[Test.cpp]
#include "SL_List.h"

int main(void)
{//Begin
SL_List<int> MyList;
return 0;
}//End



##### Share on other sites
IIRC, you cannot seperate the implementations of templates. That's why it broke when you moved it and cannot fix it. I cannot remember the proper term its called when this happens.

##### Share on other sites
Unless your compiler supports the standard export keyword (which virtually no compiler does) templates must be defined in the same translation unit they where declared in. Basically means you need to define them all in the header, you can still define them outside the class but needs to be in the header. The closest you can get to that is to put the definitions in any old file then include the file at the bottom of the header its still not the same though.

Also if you wasn't aware the standard libary provides a list type which is implementated with a doubly linked list, there is also a standard library extension called slist which is a list implementated with a singly linked-list but it might not be supported on all compilers as its an extension not currently part of the standard.

##### Share on other sites
Only Comeau supports export. Most agree that it should be deleted from the standard in the next revision.

If you want to break up your files so the declarations are separate from the implementations you can have the implementations in a file and include it from the bottom of your .hh. I don't know anyone who does this.

##### Share on other sites
Quote:
 Original post by Xorcist[Test.cpp]#include "SL_List.h"int main(void) {//Begin SL_List MyList; return 0; }//End

the declaration (and definition) of MyList in the above code requires that SL_List< int > be instantiated. the compiler must have the complete definition available at that point. the linker can not do this for us. so like the others said you can not separate the 'implementation' in the case of templates.

_ugly

• 23
• 10
• 19
• 15
• 14