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