home *** CD-ROM | disk | FTP | other *** search
- From: ravinc@hpdmd48.boi.hp.com (Ravin Chauhan (temp))
- Date: Thu, 20 Aug 1992 20:21:21 GMT
- Subject: Strange problem with Borland C++
- Message-ID: <15150008@hpdmd48.boi.hp.com>
- Organization: Hewlett Packard - Boise, ID
- Path: sparky!uunet!usc!sdd.hp.com!hpscdc!hplextra!hpcc05!hpdmd48!ravinc
- Newsgroups: comp.lang.c++
- Lines: 74
-
- 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 &)'
-
-
-
-
- /******************** START OF SOURCE CODE *********************/
- #include <string.h>
- #include <fstream.h>
- #include <iostream.h>
-
-
- /* CLASS DEFINITION WITHOUT ERROR - NOTICE NO ARGUMENT TO CONSTRUCTOR*/
- /*class Header
- {
- public:
- Header(); //constructor to init all above fields
- friend ifstream& operator>>(ifstream& ifs, Header& hdr);
- friend ofstream& operator<<(ofstream& ofs, Header& hdr);
- };
-
- Header::Header()
- {
- }
- */
-
-
- /*****CLASS DEFINITION CAUSING ERROR - NOTICE ARGUMENT TO CONSTRUCTOR *****/
- class Header
- {
- public:
- Header(int);
- friend ifstream& operator>>(ifstream& ifs, Header& hdr);
- friend ofstream& operator<<(ofstream& ofs, Header& hdr);
- };
-
- Header::Header(int i)
- {
- };
-
-
-
- ifstream& operator>>(ifstream& ifs, Header& hdr)
- {
- char empty;
- ifs >> empty;
- return (ifs);
- };
-
- ofstream& operator<<(ofstream& ofs, Header& hdr)
- {
- char empty = '@';
- ofs << empty;
- return (ofs);
- };
- /******************* END OF SOURCE CODE *************************/
-
-