Archived

This topic is now archived and is closed to further replies.

Template class in a DLL -----Linker problems

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

Recommended Posts

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?

Share on other sites
What does the header file that defines the interface to CList look like?

Share on other sites
Something like this (still incomplete)

  #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 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?

Share on other sites
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.)

Share on other sites
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:

  #ifndef XYZ_H_#define XYZ_H_template 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

Share on other sites
OK that worked but now I''m just TOTALLY confused.Why does this happen? (Specifically I mean)

Share on other sites
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)

Share on other sites
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.

1. 1
2. 2
Rutin
16
3. 3
4. 4
5. 5

• 26
• 11
• 9
• 9
• 11
• Forum Statistics

• Total Topics
633704
• Total Posts
3013460
×