home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: sparky!uunet!zaphod.mps.ohio-state.edu!rpi!utcsri!skule.ecf!torn!watserv2.uwaterloo.ca!watmath!watcgl!bambam.uwaterloo.ca!bhickey
- From: bhickey@bambam.uwaterloo.ca (Bruce Hickey)
- Subject: Templating pointers
- Message-ID: <Btyqtq.HA@watcgl.uwaterloo.ca>
- Sender: news@watcgl.uwaterloo.ca (USENET News System)
- Organization: Computer Graphics Laboratory, University of Waterloo, Ontario, Canada
- Date: Wed, 2 Sep 1992 18:17:49 GMT
- Lines: 80
-
- Hi,
-
- I have a problem converting some simple C++ code to accept templates. I want
- to pass a pointer to a templated class to a function. Sadly the templated
- functon does not appear to expand its arguments. The "standard C++" code
- below works fine. When I attempt to introduce templates I get the error
-
- "test.cc", line 25: error: use of template function freefunc() does not match any of its template definitions
- "test.cc", line 25: error: no standard conversion of struct mine <int >* to mine*
- "test.cc", line 25: error: bad argument 2 type for freefunc(): void (mine__pt__2_i::*)(int ) ( void (mine::*)(int ) expected)
- 3 errors
-
- These error messages make me think that the function freefunc() is not being
- instantiated with <int>. Do I have to change something in this code to force
- this required template expansion?
-
- Thanks,
- Bruce Hickey
-
- ------------------------ Standard C++ code - cfront 3.0.1 -------------------
- // Thanks to jamshid@emx.cc.utexas.edu and fjh@munta.cs.mu.OZ.AU for their
- // help with pointers to member functions.
- #include <iostream.h>
-
-
- class mine {
- public:
- void func( int );
- };
-
- void mine::func( int var1 )
- {
- cerr << var1 << "\n";
- }
-
- void freefunc( mine* obj, void (mine::*f)(int) )
- {
- cerr << "calling func\n";
- (obj->*f)(2);
- }
-
-
- int main()
- {
- mine test;
- mine* mineptr = &test;
-
- freefunc(mineptr, &(mine::func));
-
- }
-
- ----------------------- Templated C++ code - cfront 3.0.1 -------------------
-
- #include <iostream.h>
-
- template<class T>
- class mine {
- public:
- void func( int );
- };
-
- template<class T> void mine<T>::func( int var1 ) { cerr << var1 << "\n"; }
-
- template<class T>
- void freefunc( mine<T>* obj, void (mine<T>::*f)(int) )
- {
- cerr << "calling func\n";
- (obj->*f)(2);
- }
-
-
- int main()
- {
- mine<int> test;
- mine<int>* mineptr = &test;
-
- freefunc(mineptr, &(mine<int>::func));
-
- }
-
-