home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / lang / cplus / 11774 < prev    next >
Encoding:
Text File  |  1992-07-29  |  2.2 KB  |  113 lines

  1. Path: sparky!uunet!olivea!hal.com!darkstar.UCSC.EDU!arapaho!zien
  2. From: zien@arapaho.ucsc.edu (Jason Yeoung Zien)
  3. Newsgroups: comp.lang.c++
  4. Subject: Priblem with g++ & templates
  5. Keywords: templates g++
  6. Message-ID: <1992Jul29.221503@arapaho.ucsc.edu>
  7. Date: 30 Jul 92 05:21:59 GMT
  8. Organization: UC Santa Cruz CIS/CE
  9. Lines: 101
  10. NNTP-Posting-Host: arapaho.ucsc.edu
  11.  
  12. I have been having problems with templates and g++.  I have a program
  13. which compiles fine using ATT CC, but not with g++.  Does anyone
  14. have any hints on how I might get this to work?
  15.  
  16.  
  17. Also, is there an easy way of getting separate compilation with templates
  18. to work?  Notice that in my "tem.h" file, I have just used
  19. a #include "tem.C".  
  20.  
  21.  
  22. tem.h
  23. --------------
  24. // template<class T> class mytemp;
  25.  
  26. #ifndef _my_temp_class_
  27. #define _my_temp_class_
  28.  
  29. template<class T> 
  30. class mytemp {
  31.  private:
  32.   T value;
  33.  
  34.  public:
  35.   mytemp(T v) {
  36.     value=v;
  37.   }
  38.  
  39.   T Get() { return(value); }
  40.  
  41.   void Add(mytemp < T > & other);
  42.  
  43.   void Print();
  44.  
  45. };
  46.  
  47.  
  48. #include "tem.C"
  49.  
  50. #endif _my_temp_class_
  51.  
  52. ###############################
  53.  
  54. tem.C
  55. --------
  56.  
  57.  
  58. #include "tem.C"
  59.  
  60. #endif _my_temp_class_
  61. lancelot 26> cat tem.C
  62.  
  63. template<class T> void mytemp<T>::Print() {
  64.   cout << "val=" << value << "\n";
  65. }
  66.  
  67.  
  68. template < class T >  void mytemp<T>::Add(mytemp < T > & other) {
  69.   value += other.Get();
  70. }
  71.  
  72. #################################
  73.  
  74. test.C
  75. --------
  76.  
  77. #include "tem.h"
  78. #include <stream.h>
  79.  
  80. main() {
  81.  mytemp<int> ival(3);
  82.  mytemp<double> val(999.999), val2(88.33);
  83.  
  84.  ival.Print();
  85.  val.Print();
  86.  val.Add(val2);
  87.  val.Print();
  88.  
  89. }
  90.  
  91. #######################################
  92.  
  93.  
  94. Results
  95. ----------
  96. arapaho 21> CC test.C
  97. CC  test.C:
  98. cc  -L/usr/local/C++3.0/lib   test.c -lC
  99.  
  100. #  (above) CC worked fine....
  101.  
  102. arapaho 22> g++ test.C
  103. In file included from tem.h:28, from test.C:2:
  104. tem.C:7: parse error before `>'
  105. tem.h:21: parse error before `>'
  106. tem.h:21: parse error before `>'
  107. tem.C: In method `void  mytemp<double>::Add (...)':
  108. tem.C:8: `other' undeclared (first use this function)
  109. tem.C:8: (Each undeclared identifier is reported only once
  110. tem.C:8: for each function it appears in.)
  111. tem.C: In method `void  mytemp<int>::Add (...)':
  112. tem.C:8: `other' undeclared (first use this function)
  113.