Trouble with phone number class

This is a discussion on Trouble with phone number class within the C++ Programming forums, part of the General Programming Boards category; This program is taken straight from the Deitel book, figure 8.3. But I can't seem to get it to compile: ...

  1. #1
    Registered User Kayoss's Avatar
    Join Date
    Sep 2005
    Location
    California
    Posts
    53

    Trouble with phone number class

    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.
    THE redheaded stepchild.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,318
    >> figure 8.3

    Did you type it correctly? Are you sure the braces you typed for the member variables aren't square brackets? Did the error have a line number? Did you look closely at that line to see if you could spot the error?

  3. #3
    Registered User Kayoss's Avatar
    Join Date
    Sep 2005
    Location
    California
    Posts
    53
    Ah yes, I had curly braces instead of square brackets. My eyes must be going; thank you very much.
    THE redheaded stepchild.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Number Guessing
    By blacknapalm in forum C Programming
    Replies: 2
    Last Post: 10-01-2008, 01:48 AM
  2. Replies: 3
    Last Post: 10-31-2005, 11:05 AM
  3. strtok() a phone number
    By Sure in forum C Programming
    Replies: 3
    Last Post: 06-27-2005, 06:46 PM
  4. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM
  5. Difficulty superclassing EDIT window class
    By cDir in forum Windows Programming
    Replies: 7
    Last Post: 02-21-2002, 04:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21