• Advertisement

Archived

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

template classes causing link errors...

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

Share this post


Link to post
Share on other sites
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
i tried it out, it''s actually _export.
but now the linker complains that this is an obsolete keyword... weird, my c++ book claims it would be export, but that even doesn''t exist.
anybody?

Share this post


Link to post
Share on other sites
Whether you need to include the template function/class definition and code in the same file is compiler dependent. Visual C++ 6.0 can handle them in seperate files. Other compilers require that they be in the same file.

Michael L. Roberts
aka milo
mlbobs@telocity.com

Share this post


Link to post
Share on other sites
well, actually MY visual 6.0 gives me linker error if i put them in seperate files. that''s because templates are not accessable from other compiler units unless you declare them as export.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

  • Advertisement