Article View: gnu.g++.help
Article #884Re: Operator Overloading - Result not Expected!
From: Oliver Kurth
Date: Tue, 16 Jan 1996 10:04
Date: Tue, 16 Jan 1996 10:04
120 lines
4159 bytes
4159 bytes
Arthur Cinader wrote:
>
> I am teaching myself to program with "How To Program C++" by
> Deitel & Deitel. A great book. In chapter eight, there is a
> an example in the text on operator overloading. When I code
> the example, exactly as it appears in the book, I don't get
> the output that they show...
>
> Using g++ v2.4 on BSDI I compile with:
>
> g++ -o fig8.3 fig8.3.C
>
> here is the source:
>
> ********begin source
> // FIG8_3.CPP
> // Overloading the stream-insertion and
> // stream-extraction operators.
> #include <iostream.h>
>
> class PhoneNumber {
> friend ostream &operator<<(ostream &, const PhoneNumber &);
> friend istream &operator>>(istream &, PhoneNumber &);
> private:
> char areaCode[4]; // 3-digit area code and null
> char exchange[4]; // 3-digit exchange and null
> char line[5]; // 4-digit line and null
> };
>
> // Overloaded stream insertion operator (cannot be
> // a member function).
> ostream &operator<<(ostream &output, const PhoneNumber &num)
> {
> output << "(" << num.areaCode << ") "
> << num.exchange << "-" << num.line;
>
> return output; // enables cout << a << b << c;
> }
>
> // overloaded stream extraction operator
> istream &operator>>(istream &input, PhoneNumber &num)
> {
> input.ignore(); // skip (
> input.getline(num.areaCode, 4); // input area code
> input.ignore(2); // skip ) and space
> input.getline(num.exchange, 4); // input exchange
> input.ignore(); // skip dash (-)
> input.getline(num.line, 5); // input line
>
> return input; // enables cin >> a >> b >> c;
> }
>
> main()
> {
> PhoneNumber phone; // create object phone
>
> cout << "Enter a phone number in the "
> << "form (123) 456-7890:" << endl;
>
> // cin >> phone invokes operator>> function by
> // issuing the call operator>>(cin, phone).
> cin >> phone;
>
> // cout << phone invokes operator<< function by
> // issuing the call operator<<(cout, phone).
> cout << "The phone number entered was:" << endl
> << phone << endl;
> return 0;
> }
>
> *******end source
>
> Here is the execution:
>
> % ./fig8.3
> Enter a phone number in the form (123) 456-7890:
> (800) 555-1212
> The phone number entered was:
> (800) -
> %
>
> The output should be the full number.
>
> I tested the value of the PhoneNumber object with
> printExchange() and printLine() functions. Both print
> funtions printed nothing leading me to conclude that the
> culprit is the function:
>
> friend istream &operator>>(istream &, PhoneNumber &);
>
> I am stumped! Is there a switch I need to throw to make
> operator overloading work with g++? A search of the man pages
> has turned up nothing.
>
> Thnaks for any light you can shed!
>
> Best Regards,
>
> Arthur
> acinader@panix.com
Try out this for your input function:
// overloaded stream extraction operator
istream &operator>>(istream &input, PhoneNumber &num)
{
input.ignore(); // skip (
input.get(num.areaCode, 4, ')'); // input area code
input.ignore(2); // skip ) and space
input.get(num.exchange, 4, '-'); // input exchange
input.ignore(); // skip dash (-)
input.getline(num.line, 5); // input line
return input; // enables cin >> a >> b >> c;
}
The function getstring seems to read the whole line, so it was 'eaten'
when you read your areaCode.
Oliver Kurth
Technical Details
Message-ID:
<30FB782D.2A0@uni-sw.gwdg.de>Path:
arc1-ca.pugleaf.net!news.gwdg.de!newsReferences:
<4dedk3$fo2@panix.com>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!news.gwdg.de!news From: Oliver Kurth <okurth@uni-sw.gwdg.de> Newsgroups: gnu.g++.help,comp.lang.c++ Subject: Re: Operator Overloading - Result not Expected! Date: Tue, 16 Jan 1996 11:04:29 +0100 Organization: GWDG, Goettingen Message-ID: <30FB782D.2A0@uni-sw.gwdg.de> References: <4dedk3$fo2@panix.com> NNTP-Posting-Host: speedy.uni-sw.gwdg.de Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Mailer: Mozilla 2.0b5 (X11; I; HP-UX A.09.03 9000/735) To: Arthur Cinader <acinader@panix.com>