LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1194|回复: 5

看的是 c/c++ 程序设计大全(C how to programming 2rd), 碰到 c++ 例题过不去,有关运

[复制链接]
发表于 2003-8-19 18:44:07 | 显示全部楼层 |阅读模式

  1. // overload >> and <<
  2. #include <iostream>
  3. using namespace std;
  4.                                                                                 
  5. class PhoneNumber {
  6.         friend ostream &operator<< (ostream &, const PhoneNumber &);
  7.         friend istream &operator>> (istream &, PhoneNumber &);
  8.                                                                                 
  9. private:
  10.         char areaCode [4];
  11.         char exchange [4];
  12.         char line [5];
  13. };
  14.                                                                                 
  15. ostream &operator<< (ostream &output, const PhoneNumber &num)
  16. {
  17.         output << "(" << num.areaCode << ")"
  18.                << num.exchange << "-" << num.line;
  19.                                                                                 
  20.         return output;
  21. }
  22.                                                                                 
  23. istream &operator>> (istream & &input, phoneNumber &num)
  24. {
  25.         input.igore ();
  26.         input.getline (num.areaCode, 4);
  27.         input.ignore (2);
  28.         input.getline (num.exchange, 4);
  29.         input.ignore ();
  30.         input.getline (num.line, 5);
  31.                                                                                 
  32.         return input;
  33. }
  34.                                                                                 
  35. main ()
  36. {
  37.         PhoneNumber phone;
  38.                                                                                 
  39.         cout << "Enter a phone number in the"
  40.              << "form (123) 456-7890: \n";
  41.         cin >> phone;
  42.                                                                                 
  43.         cout << "The phone number entered was: \n"
  44.              << phone << endl;
  45.         return 0;
  46. }
复制代码

书比较老,错误提示:
$ 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
。。。。。。
 楼主| 发表于 2003-8-19 18:57:28 | 显示全部楼层
sorry, using namespace std 没有加上,光顾看后面了。
已经订正。
不过还是没能通过。
发表于 2003-8-19 19:23:19 | 显示全部楼层
在gcc中main 前面必须是int
 楼主| 发表于 2003-8-19 19:55:01 | 显示全部楼层
这个默认就是 int. 错误找到:
istream &operator>> (istream & &input, phoneNumber &num)

= >

istream &operator>> (istream & input, phoneNumber &num)

印刷错误,害人呀!
发表于 2003-8-19 21:06:23 | 显示全部楼层
这种错误编译器都会给出提示的,你仔细看看就很容易看出来的
发表于 2003-8-19 22:35:19 | 显示全部楼层
最初由 quanliking 发表
这个默认就是 int. 错误找到:
istream &operator>> (istream & &input, phoneNumber &num)

= >

istream &operator>> (istream & input, phoneNumber &num)

印刷错误,害人呀!

印刷错误是常有的事,很多书上的例子程序一般都不会一次通过。呵呵。。。
把错误找出来,不就多了一次锻炼机会吗,有所失也必有所得嘛。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表