delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/09/25/15:56:22

From: ryot AT bigfoot DOT com (George Ryot)
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Templates? What templates?
Message-ID: <37ee0684.4909821@news.clara.net>
References: <Pine DOT SUN DOT 3 DOT 91 DOT 990923112447 DOT 10652b-100000 AT is> <7sgp47$svt$1 AT vixen DOT cso DOT uiuc DOT edu> <37ed04ed DOT 4818379 AT news DOT clara DOT net> <7shdud$fnl$1 AT vixen DOT cso DOT uiuc DOT edu>
X-Newsreader: Forte Agent 1.5/32.452
X-No-Archive: yes
MIME-Version: 1.0
Date: Sat, 25 Sep 1999 19:14:44 GMT
NNTP-Posting-Host: 195.8.91.69
X-Complaints-To: abuse AT clara DOT net
X-Trace: nnrp3.clara.net 938286884 195.8.91.69 (Sat, 25 Sep 1999 20:14:44 BST)
NNTP-Posting-Date: Sat, 25 Sep 1999 20:14:44 BST
Lines: 73
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

> Yes, I tried it in CC, and it works fine. G++ gives an error like:
> main.o(.text+0xe):main.C: undefined reference to `Array<int>::Array(void)'

That is exactly the error I would expect given the example code you
posted.  I can't say that I fully understand the issues of templates,
but in summary of the threads about this that regularly appear in the
C++ newsgroups: 

there is a standards-compliant way to arrange class templates in the
same way as a normal class but as yet no compiler supports it,

some compilers allow you to do this using non-standard techniques
(Sun's repository is apparently an example of this, is that what you
are using by any chance?),

there are various work-arounds to the problem, and everybody has their
own preference! :-)

not everybody agrees on how to implement the standard so the situation
may become more confused as compiler writers start to do this! :-(

> And what do you mean "move the template definition?" I've tried commenting
> out template<class Etype> in the array.C file, and it still gave me the same
> error. Specifically what changes would you recommend?

Sorry, I meant place the whole of your class template in the header:

> >> in my array.h file:
> >>
> >> template<class Etype>
> >> class Array
> >> {
> >> public:
> >>    Array(void);
> >>
> >> private:
> >>    Etype y;
> >> };

Also in array.h ...

> >> template<class Etype>
> >> Array<Etype>::Array(void)
> >> {
> >>    y = 0;
> >> }


> >> in main.C:
> >>
> >> #include "array.h"
> >>
> >> int main(void)
> >> {
> >> Array<int> x;
> >>
> >> return 0;
> >> }

IOW array.C is not required, the whole class template goes in array.h
and is instantiated at compile time, there is no array.o to link with.

Another way is to put the member function templates in the .C file (as
you had) and then #include this at the *end* of the .h file.

And there are other (equally messy) work-arounds.

You might also want to consider using .cpp instead of .C unless this
causes problems on your other platform.  Windoze sometimes isn't too
clever about the case of file names.
-- 
george

- Raw text -


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