Jump to content
  • Advertisement

Archived

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

noxa

template classes causing link errors...

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

OT: is it just me or do you want to hurt little white bunnies when you get a link error? ;-) Ok, my problem is this: I''m writing a linked list class for my project that uses templates (that way I only write it once, duh), but for some reason it''s giving me link errors! (the function can''t be found or whatnot, C2001 from VC++). My code looks like this: template class CList { void Lala(_Type param); }; template void CList<_Type>::Lala(_Type param) { return; } (of course this isn''t real code, but it''s basically what I have) Does anyone see anything wrong with this? I don''t get it... Any help would be appreciated, and I''m sure the bunnies would thank you too :-) ~noxa~

Share this post


Link to post
Share on other sites
Advertisement
Don''t forget to put the body of the function in the header with the class.


Josh
http://www.jh-software.com

Share this post


Link to post
Share on other sites
I think thats my problem! I have the definition in a .h and the bodies in a .cpp! Why would that be a factor? Just some rule the ANSI guys made up to make a programmers life harder? :-)

Share this post


Link to post
Share on other sites
No, it''s not just to make your life harder (though sometimes I wonder.....)

There is no way for the compiler to compile a template, because there is missing information.. ie, to create a local variable of type T, the compiler has to allocate sizeof(T) bytes on the stack.. and there''s no way for it to generate any code when it can''t determine the size of the type. Also, how would it know how to do T + T, or something like that if it doesn''t know what T is yet?

So anyway, you should have the definition handy any time you want to create a new instance of the template so that the compiler can know how to generate code for it.. hmm.. I''m not sure why it didn''t give a compile-time error though.. it could easily have been caught there I think

Share this post


Link to post
Share on other sites
It wasn''t a compile eror because it was possible that somewhere else in your program you could have explicitly instantiated the template type that you wanted in some file that contains access to the definitions of the member functions.

Torval

Share this post


Link to post
Share on other sites
Template functions are similar to inline function in this respect -- you must place both in the .h file and not the .cpp file. For inline functions, this is because they require a copy of the function every time, instead of just compiling the .cpp file once into an .obj file. For template functions, it is the same principle because code for different Types (as in template< class Type >) will generate a whole other object. The functions must be handy at all times. I think this is because of the separation of the compiler and linker into different programs.



- null_pointer
Sabre Multimedia

Share this post


Link to post
Share on other sites
if you don't want the extra overhead created thru inline templates, you can just export them.

//swap.h
template swap(T&, T&);

//swap.cpp
export template swap(T&, T&)
{
//...
}

//something.cpp
include swap.h

swap(i,j);


ps: i KNOW the html generator of the board will screw up the pseudo code, so don't blame me if it does so

Edited by - Ridcully on 4/13/00 2:42:44 PM

Share this post


Link to post
Share on other sites
Put the body of the function in the header of the class, because ( I''m not sure ) is the preprocessor who works on the template.

Share this post


Link to post
Share on other sites
Here's your problem, and I think that it isn't entirly obvious. I pounded my head against my monitor for two days over this error. Simply include this file in your source:

afxtempl.h


it is not included with stdafx.h (like most other headers seem to be) and it isn't shown in any code samples I've ever seen.

I hope this fixes it. No, your not crazy. Just poor documentation.

Edited by - Procrustes on 4/14/00 2:21:28 AM

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!