Template class in a DLL -----Linker problems
Hey again people.
OK,in my engine project (which compiles to a DLL) there is a Linked List template class.I''m sure most of you know what linked lists are and how they work.But that''s not the problem.In my engine class (engine.h) I declare a private pointer to a Linked List class of type CActor like so:
CList *ActorList;
Everything compiles fine so far.
Now,in my main cpp file (engine.cpp) in the Init() function I write something like this:
ActorList = new CList();
When I try to compile this,I get the following linker error:
Enigma.obj : error LNK2001: unresolved external symbol "public: __thiscall CList::CList(void)" (??0?$CList@VCActor@@@@QAE@XZ)
Debug/Enigma.dll : fatal error LNK1120: 1 unresolved externals
Error executing xilink6.exe.
All the files are there and all the other classes compile and link properly (they are NOT template classes.The only template class that I have is this CList class)
Does anyone have any idea what''s going on?
Something like this (still incomplete)
Please not that I do NOT want to export this class outside of the DLL.This class is to be used internally by all the other classes in the DLL.
Any thoughts?
#if !defined(AFX_ENIGLIST_H__21A98ECD_6A8F_48A0_99F9_FD9DD54170C7__INCLUDED_)#define AFX_ENIGLIST_H__21A98ECD_6A8F_48A0_99F9_FD9DD54170C7__INCLUDED_#if _MSC_VER > 1000#pragma once#endif // _MSC_VER > 1000//----------------------------------------------------------------------------template <class T> class CList {public: CList(); virtual ~CList(); void InsertNode(int ID);public: T *CreateNode(void);public: T **Root;};//----------------------------------------------------------------------------#endif // !defined(AFX_ENIGLIST_H__21A98ECD_6A8F_48A0_99F9_FD9DD54170C7__INCLUDED_)
Please not that I do NOT want to export this class outside of the DLL.This class is to be used internally by all the other classes in the DLL.
Any thoughts?
I thought it might be something like this.... by far the easiest way that you can rid of that linker error is to put the constructor code in the class definition. (Has to do with the relationship between inline functions and templates.)
Yeah, thats right, you cannot have any normal .cpp files when dealing with template classes. All the information must be defined in the header file. However if you want to keep the definition and the implementation to be seperate just define a file like "xxx.cc" and #include it like so:
I''m just reiterating SiCrane there, but with some more info
------------------------------
BCB DX Library - RAD C++ Game development for BCB
#ifndef XYZ_H_#define XYZ_H_template <class T> XYZ {...};#include "XYZ.cc"#endif
I''m just reiterating SiCrane there, but with some more info
------------------------------
BCB DX Library - RAD C++ Game development for BCB
OK that worked but now I''m just TOTALLY confused.Why does this happen? (Specifically I mean)
Templates are like elaborate #define macros
Until you use it on something it doesn''t produce any code.
So if a .cpp contains a bunch of templatized methods, the .obj it produces will be empty
In fact MSVC, and nearly all other compilers, will not even (cannot even?) check the code for syntax errors until you use it on something.
I usually toss the whole template into a ''special'' .hpp file and include it just like a header.
So you could stick a
extern "C" CList<int> exportedList;
in a .dll or .cpp
but not without the <int>
...
and you can use & g t -and/or- & l t
without the spaces to get > < symbols to actually show up.
(the board/browser thinks they''re bad html tags otherwise)
Until you use it on something it doesn''t produce any code.
So if a .cpp contains a bunch of templatized methods, the .obj it produces will be empty
In fact MSVC, and nearly all other compilers, will not even (cannot even?) check the code for syntax errors until you use it on something.
I usually toss the whole template into a ''special'' .hpp file and include it just like a header.
So you could stick a
extern "C" CList<int> exportedList;
in a .dll or .cpp
but not without the <int>
...
and you can use & g t -and/or- & l t
without the spaces to get > < symbols to actually show up.
(the board/browser thinks they''re bad html tags otherwise)
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
In most instances the compiler cannot do syntax checks. For sufficiently weird operator overloading almost any code that you didn''t think syntactically valid can be.
The HTML standard requires a semicolon at the end of the & l t and & g t. IE will still display the & l t as a less than sign but Netscape won''t.
The HTML standard requires a semicolon at the end of the & l t and & g t. IE will still display the & l t as a less than sign but Netscape won''t.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement