template classes causing link errors...
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~
----------------------------noxa - Ben Vanikhttp://24.3.123.4----------------------------
Hmm... looked at your post and couldn''t find any errors, but if you''re using MFC, try using a different name for your class. Also, make sure you are defining the functions in the header file instead of the .cpp file.
- null_pointer
Sabre Multimedia
- null_pointer
Sabre Multimedia
Don''t forget to put the body of the function in the header with the class.
Josh
http://www.jh-software.com
Josh
http://www.jh-software.com
Joshhttp://www.jh-software.com
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? :-)
----------------------------noxa - Ben Vanikhttp://24.3.123.4----------------------------
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
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
adamm@san.rr.com
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
Torval
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
- null_pointer
Sabre Multimedia
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
//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
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.
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
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
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement