delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/10/21/11:32:45

From: "Andreas Bierhals" <bierhals AT studserv DOT stud DOT uni-hannover DOT de>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Linking Template classes?
Date: 18 Oct 1997 23:54:12 GMT
Organization: RRZN - Newsserver
Lines: 37
Message-ID: <01bcdc21$09655a80$LocalHost@bierhals>
References: <343D29AB DOT 6F89BB31 AT pentek DOT com>
NNTP-Posting-Host: h19.stud.uni-hannover.de
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Charles Krug <charles AT pentek DOT com> schrieb im Beitrag
<343D29AB DOT 6F89BB31 AT pentek DOT com>...
> I've a template class that compiles but won't link.
> 
> ...

Hi Charles!

If you declare a template class in a header file
(e.g. template.h)
and separate the function definitions using an implementation
file (e.g. template.cc), the compiler won't automatically
create template class instantiations for all required types. 
Now, there are two possibilities:

i) Put the declarations and definitions together in one file.
   This is not the most elegant method, it has the disadvantage,
   that you always need to include the complete source of the
   template class, where it is required. This is likely to increase
   the size of your executable, especially, when different source
   files use your template class with the same type.
ii) Supposed, that 'template.h' / 'template.cc' define the 
   class 'Test' and you
   need this class for the types 'double' and 'complex'.
   Then you should create a file like this:

   #include"template.cc"  // template.cc should include template.h

   template class Test<double>;
   template class Test<complex>;

   This file must be compiled and linked to the other objects 
   of your project.
   
               Sincerely

   Andreas B.

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019