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

  1. Path: sparky!uunet!cs.utexas.edu!sdd.hp.com!caen!destroyer!ubc-cs!mala.bc.ca!epp
  2. From: epp@mala.bc.ca (Lorne Epp)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Strange problem with Borland C++
  5. Message-ID: <1992Aug21.153832.694@mala.bc.ca>
  6. Date: 21 Aug 92 15:38:32 -0700
  7. References: <15150008@hpdmd48.boi.hp.com>
  8. Organization: Malaspina College
  9. Lines: 34
  10.  
  11. In article <15150008@hpdmd48.boi.hp.com>, ravinc@hpdmd48.boi.hp.com (Ravin Chauhan (temp)) writes:
  12. > Can someone please help me with a strange problem in Borland C++?
  13. > Defining a class' constructor with arguments has affected compilation of
  14. > operator overloading friend functions. In the following program, there
  15. > are two class definitions for Header. The first one, (without argument
  16. > to the constructor) compiles successfully. The second one, (with argument
  17. > int i to the constructor) results in two errors at compile time, in
  18. > overloaded operator function definitions ifstream& operator>>() and
  19. > ofstream& operator<<().
  20. > The following program was compiled using Borland C++ version 3.1.
  21. > The source code has been edited for readability. Ignore the warnings
  22. > and look out for the following 2 errors.
  23. > Error 1: Ambiguity between 'operator >>(ifstream &, Header &)' and
  24. >                 'istream::operator >>(unsigned char &)'
  25. > Error 2: Ambiguity between 'operator >>(ofstream &, Header &)' and
  26. >                 'ostream::operator >>(unsigned char &)'
  27. Your constructor-with-an-argument defines a conversion from int to
  28. Header.  What your compiler is telling you is that an expression such as
  29.  
  30.     unsigned char c;
  31.     cout << ch;
  32.     
  33. is now ambiguous because the compiler can't tell whether ch should be
  34. converted to a Header, and your friend function called, or left as
  35. is and ostream's operator called.  You can rewrite your code
  36. so that you explicitly call the appropriate operator function. This
  37. takes all the fun out of overloading operators, though.
  38. ----------------------------------------------------------------------
  39. Lorne Epp                                               epp@mala.bc.ca
  40.