Archived

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

Templates (Help!)

This topic is 5594 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

When I write my template classes in seperate .h and .cpp files I get this linkage errors, when I write the same class in the file where main() is located it works just fine. Why? What should I do? Thanks! My Errors: main.obj : error LNK2001: unresolved external symbol "public: __thiscall CArray::~CArray(void)" (??1?$CArray@H@@QAE@XZ) main.obj : error LNK2001: unresolved external symbol "public: __thiscall CArray::CArray(void)" (??0?$CArray@H@@QAE@XZ)

Share this post


Link to post
Share on other sites
Is Carray the win32 built -in class ? is it a class of yours ?
i think you should put the include file on top of your main .h file and add the files ti the projects , once i had the same problem with stl and it worked, i hope it helps....

Share this post


Link to post
Share on other sites
Your template declaration and implementation have to be in the header file. You cannot put your implementation in a seperate source file. I cannot remember exactly why but I am sure someone else can tell you exactly why.

Share this post


Link to post
Share on other sites
I has that problem too. There are a pragma option (preprocessor directives) which permit separate the class implementation and class definition in two files. I dont remember how to do it becouse personally i prefer make all in the .h file. I has see an example, but i cant find it, but i remember in the pragma option must be each data tipe that you want use in the template class, so tedious.
Personally i think that is faster make all in .h file and, im not sure, i think that there are no bad consequences.
I hope you understand my english.
See you.

naher

Share this post


Link to post
Share on other sites
A minute PLZ!

i use to declare my classes in a header.h and define them in a header.cpp after that i just #include <header.h> in my main.cpp; Everything goes always fine. i''m not sure to get ur problem.

Share this post


Link to post
Share on other sites
quote:
Original post by remi
i''m not sure to get ur problem.


The problem is specific to the use of templated code. When the compiler sees a template, object code is not generated unless the template is actually *instantiated*. That means the compiler must be able to see the template when it encounters an instantiation of that template, else it will not know what it is supposed to be instantiating.

There is a keyword called "export" which supposedly allows templated code to export some intermediate representation for a later phase of compiling / linking, but EDG is the only compiler supporting the facility, and it has dubious benefits. The upshot is that templated code must be put into a #include''d file so that it can be seen. The easiest approach is to ensure all templated code is in the relevant header file.

Share this post


Link to post
Share on other sites
you can have a template class declared in a header and implmented in a .cpp file, you just have to go like this in teh cpp file

  
template<class type>
void className<type> :: funcName(parameters)
{
}

each function has to have the template line before its actual implementation thats all

Share this post


Link to post
Share on other sites
no no no, vaneger, you have to have the implementation in the .cpp file...
you can of course #include the .cpp file in the .h file...
then that will work
what you are referring to is having the function outside of the template definition...ie

  
template <class T> class D {
void C() { cout << "inside!" << endl;}
};

//as opposed to


template <class T> class D {
void C();
};

template <class T> void D<T>::C() {cout << "outside!" << endl;}





------------------------------
BCB DX Library - RAD C++ Game development for BCB - so what if I'm not working on it anymore?

[edited by - c++freak on August 18, 2002 9:31:02 PM]

[edited by - c++freak on August 18, 2002 9:31:46 PM]

[edited by - c++freak on August 18, 2002 9:32:22 PM]

Share this post


Link to post
Share on other sites
yeah, what SabreMan said....

actually, a nice way of doing it (if you really want to keep them seperate) is rename the .cpp file for your template to .tpp (or something meaningful to you) and then include it at the bottom of your header file.

this lets you keep it logically seperate, but still having it in the header when you compile.

Share this post


Link to post
Share on other sites
quote:
Original post by vaneger
you can have a template class declared in a header and implmented in a .cpp file

Which compiler have you used to confirm that will work?

Share this post


Link to post
Share on other sites