This program is taken straight from the Deitel book, figure 8.3. But I can't seem to get it to compile:
Code:
#include <iostream>
#include <iomanip>
using namespace std;
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
}; // end class PhoneNumber
ostream &operator<<( ostream &output, const PhoneNumber &num )
{
output << "(" << num.areaCode << ")" << num. exchange << "-" << num.line;
return output; // enables cout << a << b << c;
} // end function operator<<
istream &operator>>( istream &input, PhoneNumber &num )
{
input.ignore();
input >> setw( 4 ) >> num.areaCode;
input.ignore( 2 ); // skip ( and space
input >> setw( 4 ) >> num.exchange;
input.ignore();
input >> setw( 5 ) >> num.line; // input line
return input;
}
int main()
{
PhoneNumber phone; // create object phone
cout << "Enter phone number in the form (123) 456-7890:\n";
cin >> phone;
cout << "The phone number entered was: ";
cout << phone << endl;
return 0;
} // end main
I get several errors, including "invalid member function declaration"s and "class PhoneNumber has no member named 'areaCode'". I had some compiler issues earlier with a program that worked fine for others, so I'm wondering if this is related or not.