home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / lang / cplus / 12709 < prev    next >
Encoding:
Internet Message Format  |  1992-08-20  |  2.2 KB

  1. From: ravinc@hpdmd48.boi.hp.com (Ravin Chauhan (temp))
  2. Date: Thu, 20 Aug 1992 20:21:21 GMT
  3. Subject: Strange problem with Borland C++
  4. Message-ID: <15150008@hpdmd48.boi.hp.com>
  5. Organization: Hewlett Packard - Boise, ID
  6. Path: sparky!uunet!usc!sdd.hp.com!hpscdc!hplextra!hpcc05!hpdmd48!ravinc
  7. Newsgroups: comp.lang.c++
  8. Lines: 74
  9.  
  10. Can someone please help me with a strange problem in Borland C++?
  11.  
  12. Defining a class' constructor with arguments has affected compilation of
  13. operator overloading friend functions. In the following program, there
  14. are two class definitions for Header. The first one, (without argument
  15. to the constructor) compiles successfully. The second one, (with argument
  16. int i to the constructor) results in two errors at compile time, in
  17. overloaded operator function definitions ifstream& operator>>() and
  18. ofstream& operator<<().
  19.  
  20.  
  21. The following program was compiled using Borland C++ version 3.1.
  22. The source code has been edited for readability. Ignore the warnings
  23. and look out for the following 2 errors.
  24.  
  25. Error 1: Ambiguity between 'operator >>(ifstream &, Header &)' and
  26.                 'istream::operator >>(unsigned char &)'
  27. Error 2: Ambiguity between 'operator >>(ofstream &, Header &)' and
  28.                 'ostream::operator >>(unsigned char &)'
  29.  
  30.  
  31.  
  32.  
  33. /********************    START OF SOURCE CODE    *********************/
  34. #include <string.h>
  35. #include <fstream.h>
  36. #include <iostream.h>
  37.  
  38.  
  39. /* CLASS DEFINITION WITHOUT ERROR - NOTICE NO ARGUMENT TO CONSTRUCTOR*/
  40. /*class Header
  41.     {
  42.     public:
  43.         Header();            //constructor to init all above fields
  44.         friend ifstream& operator>>(ifstream& ifs, Header& hdr);
  45.         friend ofstream& operator<<(ofstream& ofs, Header& hdr);
  46.     };
  47.  
  48. Header::Header()
  49.     {
  50.     }
  51. */
  52.  
  53.  
  54. /*****CLASS DEFINITION CAUSING ERROR - NOTICE ARGUMENT TO CONSTRUCTOR *****/
  55. class Header
  56.     {
  57.     public:
  58.         Header(int);
  59.         friend ifstream& operator>>(ifstream& ifs, Header& hdr);
  60.         friend ofstream& operator<<(ofstream& ofs, Header& hdr);
  61.     };
  62.  
  63. Header::Header(int i)
  64.     {
  65.     };
  66.  
  67.  
  68.  
  69. ifstream& operator>>(ifstream& ifs, Header& hdr)
  70.     {
  71.     char empty;
  72.     ifs >> empty;
  73.     return (ifs);
  74.     };
  75.  
  76. ofstream& operator<<(ofstream& ofs, Header& hdr)
  77.     {
  78.     char empty = '@';
  79.     ofs << empty;
  80.     return (ofs);
  81.     };
  82. /*******************    END OF SOURCE CODE    *************************/
  83.  
  84.