home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!olivea!hal.com!darkstar.UCSC.EDU!arapaho!zien
- From: zien@arapaho.ucsc.edu (Jason Yeoung Zien)
- Newsgroups: comp.lang.c++
- Subject: Priblem with g++ & templates
- Keywords: templates g++
- Message-ID: <1992Jul29.221503@arapaho.ucsc.edu>
- Date: 30 Jul 92 05:21:59 GMT
- Organization: UC Santa Cruz CIS/CE
- Lines: 101
- NNTP-Posting-Host: arapaho.ucsc.edu
-
- I have been having problems with templates and g++. I have a program
- which compiles fine using ATT CC, but not with g++. Does anyone
- have any hints on how I might get this to work?
-
-
- Also, is there an easy way of getting separate compilation with templates
- to work? Notice that in my "tem.h" file, I have just used
- a #include "tem.C".
-
-
- tem.h
- --------------
- // template<class T> class mytemp;
-
- #ifndef _my_temp_class_
- #define _my_temp_class_
-
- template<class T>
- class mytemp {
- private:
- T value;
-
- public:
- mytemp(T v) {
- value=v;
- }
-
- T Get() { return(value); }
-
- void Add(mytemp < T > & other);
-
- void Print();
-
- };
-
-
- #include "tem.C"
-
- #endif _my_temp_class_
-
- ###############################
-
- tem.C
- --------
-
-
- #include "tem.C"
-
- #endif _my_temp_class_
- lancelot 26> cat tem.C
-
- template<class T> void mytemp<T>::Print() {
- cout << "val=" << value << "\n";
- }
-
-
- template < class T > void mytemp<T>::Add(mytemp < T > & other) {
- value += other.Get();
- }
-
- #################################
-
- test.C
- --------
-
- #include "tem.h"
- #include <stream.h>
-
- main() {
- mytemp<int> ival(3);
- mytemp<double> val(999.999), val2(88.33);
-
- ival.Print();
- val.Print();
- val.Add(val2);
- val.Print();
-
- }
-
- #######################################
-
-
- Results
- ----------
- arapaho 21> CC test.C
- CC test.C:
- cc -L/usr/local/C++3.0/lib test.c -lC
-
- # (above) CC worked fine....
-
- arapaho 22> g++ test.C
- In file included from tem.h:28, from test.C:2:
- tem.C:7: parse error before `>'
- tem.h:21: parse error before `>'
- tem.h:21: parse error before `>'
- tem.C: In method `void mytemp<double>::Add (...)':
- tem.C:8: `other' undeclared (first use this function)
- tem.C:8: (Each undeclared identifier is reported only once
- tem.C:8: for each function it appears in.)
- tem.C: In method `void mytemp<int>::Add (...)':
- tem.C:8: `other' undeclared (first use this function)
-