Jump to content

Help with linking error!?


pasdVn

Recommended Posts

Hey guys. I need the help of some c++ crack :P. I have the following specific problem:

I want to call "template<typename T>WorldObject* Spell::FindCorpseUsing()" with "MaNGOS::RaiseDeadObjectCheck" as type in Spelleffects.cpp. I already included "GridNotifiers.h" in Spelleffects. So it looks like that:

WorldObject* result = FindCorpseUsing<MaNGOS::RaiseDeadObjectCheck> ();

I get the following linking error:

game.lib(SpellEffects.obj) : error LNK2019: unresolved external symbol "public: class WorldObject * __thiscall Spell::FindCorpseUsing<class MaNGOS::RaiseDeadObjectCheck>(void)" (??$FindCorpseUsing@VRaiseDeadObjectCheck@MaNGOS@@@Spell@@QAEPAVWorldObject@@XZ) referenced in function "public: void __thiscall Spell::EffectScriptEffect(unsigned int)" (?EffectScriptEffect@Spell@@QAEXI@Z)
If "FindCorpseUsing<MaNGOS::RaiseDeadObjectCheck> ()" gets called in Spell.cpp before (e.g. I modified the lines in Spell::SetTargetMap(...) where target selection of "Cannibalize" is coded) the problem does not exsist. Problem exsists also with other Check-Classes (CannibalizeObjectCheck, ExplodeCorpseObjectCheck).

My c++ knowledge is too rudimentary to solve this problem :-/.

Link to comment
Share on other sites

That's because templates are just...well...templates. They don't compile to anything unless you instantiate them.

You can either implicitly instantiate it (by using it), or explicitly instantiate it. In both cases its definition has to be available of course.

Since you (only) want to use it in a place where its definition is not available, you need to explicitly instantiate it where it is available.

Although i rarely get the template syntax right in the first try, try adding something like

template WorldObject* Spell::FindCorpseUsing<MaNGOS::RaiseDeadObjectCheck>();

to Spell.cpp (somewhere after the function definition, at global scope)

Link to comment
Share on other sites

Hmm, instantiate? FindCorpseUsing is just a (template-) method of class spell declared in Spell.h:

template<typename T> WorldObject* FindCorpseUsing();

So I think I can just call it with specifying a typename - in this the type should be RaiseDeadObjectCheck of namespace MaNGOS, or am I wrong? As far as I can see the call of FindCorpseUsing<MaNGOS::RaiseDeadObjectCheck> () - in the position I mentioned in first post - is also not instantiate anywhere.

Sorry, did not work that intensive with template function/classes until now :-/

Link to comment
Share on other sites

*scratches head*

I'm not sure i can follow you...

I'll just try to explain more comprehensively.

A template is a bit like macro on steroids. It's a piece of code with some place holders to fill in. Without naming specific types, it cannot be compiled to binary code, infact, MSVC will not even check its syntax unless you instantiate it. Instantiate means creating an actual function/class from it by specifying its template parameters. For a function, each set of parameters becomes an own, distinct function that has to be compiled at some point. Most of the time, the templates are instantiated automatically, the compiler sees your template parameters, and if it hasn't created code yet goes to your template definition and actually compiles code for it.

In your case, automatic instantiation works fine in Spell.cpp, since the function is defined there. However you want to use it in another source file where the definition of the template is not available, so the compiler assumes you instantiate it somewhere else so it will be found when linking the program. However, you did not do that either, so linking fails.

So you basically have two options:

a) Move the definition of the template to a header so it becomes available in the source file where you need it, and let it automatically be instantiated

b) Explicitly tell the compiler in Spell.cpp to create functions for the template parameters your need elsewhere so it is available when linking the final binary

Link to comment
Share on other sites

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue. Privacy Policy Terms of Use