home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / lang / cplus / 16493 < prev    next >
Encoding:
Text File  |  1992-11-17  |  2.0 KB  |  73 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!charon.amdahl.com!pacbell.com!sgiblab!munnari.oz.au!titan!trlluna!bruce.cs.monash.edu.au!monu6!nella9.cc.monash.edu.au!rik
  3. From: rik@nella9.cc.monash.edu.au (Rik Harris)
  4. Subject: template problem (g++)
  5. Message-ID: <rik.722065099@nella9.cc.monash.edu.au>
  6. Keywords: bug? g++ template operator
  7. Sender: news@monu6.cc.monash.edu.au (Usenet system)
  8. Reply-To: rik.harris@fcit.monash.edu.au
  9. Organization: Monash University, Melb., Australia.
  10. Date: Wed, 18 Nov 1992 05:38:19 GMT
  11. Lines: 60
  12.  
  13. I'm having a problem with templates, using g++ 2.3.1.  It occurs when a
  14. function template argument is a base class, and a template function
  15. not being generated when using a derived class (ARM terminology p345).
  16. In the program below, I believe it should generate a template for
  17. operator<< for the A<int> type, as B is derived from A<int>, but it
  18. doesn't seem to.  I get the error:
  19. ld: Undefined symbol
  20.    ___ls__FR7ostreamRt1A1Zi
  21.  
  22.  
  23. Am I expecting too much from C++, or is this a problem with g++ 2.3.1?
  24. I have a friend with Borland C++ (a version with templates, not sure
  25. which one) and he was able to compile this code.  If it is
  26. unreasonable for C++ to automatically generate a template for operator<<,
  27. is there a way I can coerce it to do so?
  28.  
  29. // simplified example...don't look too hard at it's usefulness :-)
  30. #include    <stream.h>
  31.  
  32. template<class T>
  33. class A
  34. {
  35.   T    a;
  36. public:
  37.   void set(T b) { a=b; }
  38.   friend ostream& operator  << (ostream& s, A<T>& x);
  39. };
  40.  
  41. template<class T>
  42. ostream& operator << (ostream& s, A<T>& x)
  43. {
  44.   return s << "template: (" << x.a << ")\n";
  45. }
  46.  
  47. // it compiles with this line uncommented
  48. //ostream& operator << (ostream&s, A<int>& x) { return s; }
  49.  
  50. class B : public A<int>
  51. {
  52.   int    a;
  53. public:
  54.   void set(int i) { a=i; }
  55. };
  56.  
  57. main()
  58. {
  59.   B    b;
  60.  
  61.   b.set(3);
  62.   cout << b;
  63. }
  64.  
  65.  
  66. thanks for any help,
  67. rik.
  68. --
  69. Rik Harris - rik.harris@fcit.monash.edu.au
  70. +61 3 571-2895 (AH & ans.mach) +61 3 573-2679 (BH)
  71. Faculty of Computing and Information Technology,
  72. Caulfield Campus, Monash University, Australia
  73.