🔒 Locked
template classes causing link errors...
Advertisement
Original Post
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~
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
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
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.cpp
export template
{
//...
}
//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
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
Not that it means much, but a couple weeks ago I was stuck with exactly the same problem.
It resulted in mass confusion for a week or so until I accidentally discovered the cause. See, not having done any templates before, I was unaware that you needed the body with the header.
Confusing, eh?
regards,
GeniX
It resulted in mass confusion for a week or so until I accidentally discovered the cause. See, not having done any templates before, I was unaware that you needed the body with the header.
Confusing, eh?
regards,
GeniX
Ridcully: How do you declare them as export? The export keyword doesn''t exist in VC 6.0a, and _export meets with compiler errors saying that it is non-standard and not supported.
- null_pointer
Sabre Multimedia
- null_pointer
Sabre Multimedia
I''m not sure that VC6 supports ''export''. Very few compilers do right now (if any). After realizing the problems that templates might cause (this problem specifically) they added the export keyword that would require the implementation to be able to delay instantiation and such and link in template functions as though they were normal. Of course, since it''s probably one of the most difficult parts of the standard to implement, and since most people are used to just writing everything in the .h files, it''s not a priority for most compiler vendors.
Also, export does not seem to be an optional feature. That section of the standard (it''s littered throughout chapter 14) is somewhat vague in what export should allow, but it does make the following statement (chapter 14, paragraph 9):
Note: an implementation may require that a translation unit containing the definition of an exported template be compiled before any translation unit containing an instantiation of that template.
I wish I was a better language lawyer for these issues. Oh well...
-Brian
Also, export does not seem to be an optional feature. That section of the standard (it''s littered throughout chapter 14) is somewhat vague in what export should allow, but it does make the following statement (chapter 14, paragraph 9):
Note: an implementation may require that a translation unit containing the definition of an exported template be compiled before any translation unit containing an instantiation of that template.
I wish I was a better language lawyer for these issues. Oh well...
-Brian
well, some weeks ago i was stuck with the same problem, and looked up what my c++ game said to this, and it said you have to declare your functions with export.
i tried that, and you''re right, vc6 doesn''t support it.
so i put an underscore before it, and although it complained about it being obsolete, it compiled without linker error.
then, today, i compiled it again, and it DID NOT compile.
so i think osmanb is right, it''s not support although it should be part of the standard
i tried that, and you''re right, vc6 doesn''t support it.
so i put an underscore before it, and although it complained about it being obsolete, it compiled without linker error.
then, today, i compiled it again, and it DID NOT compile.
so i think osmanb is right, it''s not support although it should be part of the standard
In Microsoft C (and VC++), the _export keyword has been renamed to __export and then replaced by __declspec(dllexport). However these key words are used to export functions, data, etc. in a DLL without the need to add entries to a .DEF file.
Which is a totally different beast than the export keyword from the standard.
Tim
Which is a totally different beast than the export keyword from the standard.
Tim
All I can say is that I hope VC++ gets its act together with template classes by version 7.0 -- it can''t list template class member functions from a namespace in its ClassView window, and it cannot list template class sub objects'' member functionsin ClassView either. It also cannot do template class sub objects outside the class definition.
- null_pointer
Sabre Multimedia
- null_pointer
Sabre Multimedia
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement