Mail Archives: djgpp/1999/09/25/15:56:22
> 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 -