Hello there,
i have a class of custom implementation of the famous linklist using templates in a custom DLL (called A) i made. all worked out perfectly, i exported the class and everything was fine there.
in another DLL, i instansiated that linkedlist class i did over in first DLL (called B) and started using, all went there. i Compiled! i was struck with "error lnk2019" in the functions that i called linklist in. supprisingly i already included that lib file in my second DLL B. and included the main header file, that has included all headers in DLL A. so technically it has no excuse in lnk2019-ing.
here is my linklist class header
[source]
#pragma once
#include "StdAfx.h"
template <class T, unsigned int size = 10, unsigned int enlarge = 10>
class __declspec(dllexport) NLinkedList
{
// variable
T *_linkedList;
unsigned int _count;
unsigned int _capacity;
unsigned int _enlarge;
//functions
void ExpandList();
public:
NLinkedList(void) ;
~NLinkedList(void);
T operator[] (int index);
T Get(int index);
void Add(T object);
void Edit(int index, T object);
void RemoveAt(int index);
void Remove(T object);
void Clear();
unsigned int GetCount();
unsigned int GetCapacity();
};
[/source]
and here is the class implementation
[source]
#include "StdAfx.h"
#include "nlinkedlist.h"
template <class T, unsigned int size, unsigned int enlarge>
NLinkedList<T, size, enlarge>::NLinkedList(void)
{
_linkedList = new T[size];
memset(_linkedList, 0, size);
_count = 0;
_capacity = size;
_enlarge = enlarge;
}
template <class T, unsigned int size, unsigned int enlarge>
NLinkedList< T, size, enlarge>::~NLinkedList(void)
{
delete []_linkedList;
}
... (along with rest of the function implementation)
[/source]
now here comes how i use that class in my DLL B.
[source]
class __declspec(dllexport) NMap
{
NLinkedList<NEntity*, 10, 100>* _mapEntities;
NLinkedList<NEntity*, 10, 100>* _selectedEntities;
...
};
[/source]
and here is the class implementation
[source]
#include "stdafx.h"
#include "NMap.h"
#include "ngame3dDevice.h"
#include "NEntity.h"
NMap::NMap(NGameDevices* gameDevice)
{
_mapEntities = new NLinkedList<NEntity*, 10, 100>();
_selectedEntities = new NLinkedList<NEntity*, 10, 100>();
}
...
[/source]
and here is the include of the lib
[source]
#include "ncommon.h"
// load the libs
#pragma comment(lib, "ncommon.lib") // my DLL A
[/source]
i cant seem to know why it is erroring.
oh here the link error:
[source]
1>NMap.obj : error LNK2019: unresolved external symbol "public: __thiscall NLinkedList<class NEntity *,10,100>::NLinkedList<class NEntity *,10,100>(void)" ( 0 $NLinkedList@PAVNEntity@@$09$0GE@@@QAE@XZ) referenced in function "public: __thiscall NMap::NMap(class NGameDevices *)" ( 0NMap@@QAE@PAVNGameDevices@@@Z)
1>NMap.obj : error LNK2019: unresolved external symbol "public: void __thiscall NLinkedList<class NEntity *,10,100>::Clear(void)" ( Clear@ $NLinkedList@PAVNEntity@@$09$0GE@@@QAEXXZ) referenced in function "public: __thiscall NMap::~NMap(void)" ( 1NMap@@QAE@XZ)
1>NMap.obj : error LNK2019: unresolved external symbol "public: class NEntity * __thiscall NLinkedList<class NEntity *,10,100>::Get(int)" ( Get@ $NLinkedList@PAVNEntity@@$09$0GE@@@QAEPAVNEntity@@H@Z) referenced in function "public: __thiscall NMap::~NMap(void)" ( 1NMap@@QAE@XZ)
1>NMap.obj : error LNK2019: unresolved external symbol "public: unsigned int __thiscall NLinkedList<class NEntity *,10,100>::GetCount(void)" ( GetCount@ $NLinkedList@PAVNEntity@@$09$0GE@@@QAEIXZ) referenced in function "public: __thiscall NMap::~NMap(void)" ( 1NMap@@QAE@XZ)
1>NMap.obj : error LNK2019: unresolved external symbol "public: void __thiscall NLinkedList<class NEntity *,10,100>::Add(class NEntity *)" ( Add@ $NLinkedList@PAVNEntity@@$09$0GE@@@QAEXPAVNEntity@@@Z) referenced in function "public: void __thiscall NMap::AddEntity(struct NEntityConfig)" ( AddEntity@NMap@@QAEXUNEntityConfig@@@Z)
[/source]

link error 2019 with template class in dll
Farooq Hawalteh
yeah thats what i did at the very end, after trying that out!
i just hope it works out at the very end!
thx for the info
Fizban
Templates and DLL's do not work like this.
For templates to work the definition of the template and any of its members must be available in every compiland in which the template and/or one of its members is referenced.
A DLL is not a template repository it only contains the executable code for compiled functions.
Your best solution is just to place the whole definition of a template in a header file.