Template functions (linkage error)

Started by
8 comments, last by goldsword 21 years, 8 months ago
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?
#ifndef (Goldsword_was_here)#define (Goldsword_was_here)cout << "Goldsword was here" << endl;#endif
Advertisement
"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]
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..."
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
#ifndef (Goldsword_was_here)#define (Goldsword_was_here)cout << "Goldsword was here" << endl;#endif
What errors do you get?

Helpful links:
How To Ask Questions The Smart Way | Google can help with your question | Search MSDN for help with standard C or Windows functions


[edited by - siaspete on August 18, 2002 7:30:37 AM]
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!
If brute force does not solve your problem....you're not using enough!
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???
#ifndef (Goldsword_was_here)#define (Goldsword_was_here)cout << "Goldsword was here" << endl;#endif
Yes you''re right, you''ll have to put the functions in the header.

Put them inside the template class itself.

Helpful links:
How To Ask Questions The Smart Way | Google can help with your question | Search MSDN for help with standard C or Windows functions
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!
If brute force does not solve your problem....you're not using enough!
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!
神はサイコロを振らない!

This topic is closed to new replies.

Advertisement