home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / lang / cplus / 13217 < prev    next >
Encoding:
Text File  |  1992-09-02  |  2.3 KB  |  91 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!zaphod.mps.ohio-state.edu!rpi!utcsri!skule.ecf!torn!watserv2.uwaterloo.ca!watmath!watcgl!bambam.uwaterloo.ca!bhickey
  3. From: bhickey@bambam.uwaterloo.ca (Bruce Hickey)
  4. Subject: Templating pointers
  5. Message-ID: <Btyqtq.HA@watcgl.uwaterloo.ca>
  6. Sender: news@watcgl.uwaterloo.ca (USENET News System)
  7. Organization: Computer Graphics Laboratory, University of Waterloo, Ontario, Canada
  8. Date: Wed, 2 Sep 1992 18:17:49 GMT
  9. Lines: 80
  10.  
  11. Hi,
  12.  
  13. I have a problem converting some simple C++ code to accept templates. I want
  14. to pass a pointer to a templated class to a function. Sadly the templated
  15. functon does not appear to expand its arguments. The "standard C++" code
  16. below works fine. When I attempt to introduce templates I get the error
  17.  
  18. "test.cc", line 25: error: use of template function freefunc() does not match any of its template definitions
  19. "test.cc", line 25: error: no standard conversion of struct  mine <int >* to mine*
  20. "test.cc", line 25: error: bad argument  2 type for freefunc(): void (mine__pt__2_i::*)(int ) ( void (mine::*)(int ) expected)
  21. 3 errors
  22.  
  23. These error messages make me think that the function freefunc() is not being
  24. instantiated with <int>. Do I have to change something in this code to force
  25. this required template expansion? 
  26.  
  27. Thanks,
  28. Bruce Hickey
  29.  
  30. ------------------------ Standard C++ code - cfront 3.0.1 -------------------
  31. // Thanks to jamshid@emx.cc.utexas.edu and fjh@munta.cs.mu.OZ.AU for their
  32. // help with pointers to member functions.
  33. #include <iostream.h>
  34.  
  35.  
  36. class mine {
  37. public:
  38.     void func( int );
  39. };
  40.  
  41. void mine::func( int var1 ) 
  42. {  
  43.     cerr << var1 << "\n"; 
  44. }
  45.  
  46. void freefunc( mine* obj, void (mine::*f)(int) ) 
  47.     cerr << "calling func\n";
  48.     (obj->*f)(2);
  49. }
  50.  
  51.  
  52. int main()
  53. {
  54. mine test;
  55. mine* mineptr = &test;
  56.  
  57. freefunc(mineptr, &(mine::func));
  58.  
  59. }
  60.  
  61. ----------------------- Templated C++ code - cfront 3.0.1 -------------------
  62.  
  63. #include <iostream.h>
  64.  
  65. template<class T>
  66. class mine {
  67. public:
  68.     void func( int );
  69. };
  70.  
  71. template<class T> void mine<T>::func( int var1 ) { cerr << var1 << "\n"; }
  72.  
  73. template<class T>
  74. void freefunc( mine<T>* obj, void (mine<T>::*f)(int) ) 
  75.     cerr << "calling func\n";
  76.     (obj->*f)(2);
  77. }
  78.  
  79.  
  80. int main()
  81. {
  82. mine<int> test;
  83. mine<int>* mineptr = &test;
  84.  
  85. freefunc(mineptr, &(mine<int>::func));
  86.  
  87. }
  88.  
  89.