🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: gnu.g++.help
Article #881

Operator Overloading - Result not Expected!

#881
From: Arthur Cinader
Date: Mon, 15 Jan 1996 20:32
101 lines
3062 bytes
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
Technical Details
Message-ID: <4dedk3$fo2@panix.com>
Path: arc1-ca.pugleaf.net!newsfeed.internetmci.com!panix!not-for-mail

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!newsfeed.internetmci.com!panix!not-for-mail
From: acinader@panix.com (Arthur Cinader)
Newsgroups: gnu.g++.help,comp.lang.c++
Subject: Operator Overloading - Result not Expected!
Date: 15 Jan 1996 15:32:03 -0500
Organization: Panix
Message-ID: <4dedk3$fo2@panix.com>
NNTP-Posting-Host: panix.com