|
- // overload >> and <<
- #include <iostream>
- using namespace std;
-
- class PhoneNumber {
- friend ostream &operator<< (ostream &, const PhoneNumber &);
- friend istream &operator>> (istream &, PhoneNumber &);
-
- private:
- char areaCode [4];
- char exchange [4];
- char line [5];
- };
-
- ostream &operator<< (ostream &output, const PhoneNumber &num)
- {
- output << "(" << num.areaCode << ")"
- << num.exchange << "-" << num.line;
-
- return output;
- }
-
- istream &operator>> (istream & &input, phoneNumber &num)
- {
- input.igore ();
- input.getline (num.areaCode, 4);
- input.ignore (2);
- input.getline (num.exchange, 4);
- input.ignore ();
- input.getline (num.line, 5);
-
- return input;
- }
-
- main ()
- {
- PhoneNumber phone;
-
- cout << "Enter a phone number in the"
- << "form (123) 456-7890: \n";
- cin >> phone;
-
- cout << "The phone number entered was: \n"
- << phone << endl;
- return 0;
- }
复制代码
书比较老,错误提示:
$ g++ phone.cpp -o phone
phone.cpp:5: error: ISO C++ forbids declaration of `ostream' with no type
phone.cpp:5: error: `ostream' is neither function nor member function; cannot
be declared friend
phone.cpp:5: error: syntax error before `&' token
phone.cpp:6: error: ISO C++ forbids declaration of `istream' with no type
phone.cpp:6: error: `istream' is neither function nor member function; cannot
be declared friend
。。。。。。 |
|