Archived

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

Template functions (linkage error)

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Okay, I''m working on a template class but I keep getting this linkage error when I put my class in a seperate file, it works when I put the code in the main() file. Any idea?

Share this post


Link to post
Share on other sites
"this linkage error" ?? which one ??

Did you include the header of your template class definition in your main cpp file ?

__________________________



Bruno Wieckowski
Lead Programmer
Exood4 Studios


[edited by - brunow on August 17, 2002 6:28:52 PM]

Share this post


Link to post
Share on other sites
Template definitions have to be included with whatever files use them, not just the declarations -- similar to inlined functions. That''s because templated functions and classes are only created when an instances of a particular templated version of that function/class is declared. If the templated definition is not accessible during compilation, then that version of the function can''t be made.

--------------------
Matthew Calabrese
Realtime 3D Orchestra:
Programmer, Composer,
and 3D Artist/Animator
"I can see the music..."

Share this post


Link to post
Share on other sites
Well in my main.cpp i include "carray.h", in my carray.cpp file i include "carray.h" and then my code, in my "carray.h", shouldn''t that be enough? Maybe I shouldn''t use the #pragrma once directive or #ifndef xxx, #define xxx in my .h file since the compiler generates code for each type (?).

Thx

Share this post


Link to post
Share on other sites
Any function that actually uses the META parameter, must be defined in a scope where the META parameter is defined. If your class is:

template
class myclass{

A myInstanceOfA;
int func(){if(A==0) return 0; else return 1}
int func2(){retunr 4;}
}


the func must be implemented in the .h since in the .cc (or .cpp) the A class is not defined. If the functions is a template functions, then it can be done in the cpp. Any other functions like func2, could be implemented in the .cc


At least thet is the CORRECT behavior of templetes. Visual C++ does not work with templates as the standard rulez.. so I described a gcc behavior.

If brute force does not solve your problem....you''re not using enough!

Share this post


Link to post
Share on other sites
My Error Codes:

error LNK2001: unresolved external symbol "public: __thiscall CArray::~CArray(void)" (??1?$CArray@H@@QAE@XZ)

error LNK2001: unresolved external symbol "public: __thiscall CArray::CArray(void)" (??0?$CArray@H@@QAE@XZ)

// My Class (in carray.h)
template
class CArray
{
private:
type* m_tpArray;
public:
CArray();
~CArray();
};

// (in carray.cpp)
#include "carray.h"

template
CArray::CArray() : m_tpArray(0)
{}

template
CArray::~CArray()
{
delete [] m_tpArray(0)
}

// (in main.cpp)
#include "carray.h"

int main()
{
CArray test; // Error
return 0;
}

////////////////////////////////////

Must I write my entire template class in the .h file?
Can''t I use a .cpp file is that what you are saying.
But writing my functions in the .h file will make them
inline right? What if I have big functions that I don''t
want to inline? shouldn''t the include directive paste
all code from a file, if it does then why do my templates
work when I write them in my main.cpp and not when i include
my class file???

Share this post


Link to post
Share on other sites
No you wont. The functions that do not use the class parameter can be done at .cc But the name of your class is linked to the template.


int MyClass::myfunction(...) {implementation}

copy?

If brute force does not solve your problem....you''re not using enough!

Share this post


Link to post
Share on other sites
quote:
Original post by goldsword
Must I write my entire template class in the .h file?



The entire template class declaration and definition must
be contained in the .h file (or #include''d, whatever) so
that the whole class is available to the compiler.

quote:

But writing my functions in the .h file will make them
inline right? What if I have big functions that I don''t
want to inline?



You can still write out-of-line member functions for templated
classes. Just write them, err, out-of-line. Put the
definition outside of the class declaration.





Kami no Itte ga ore ni zettai naru!

Share this post


Link to post
Share on other sites