LinuxSir.cn,穿越时空的Linuxsir!

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

怎么不认识我的cout啊

[复制链接]
发表于 2003-9-26 23:16:59 | 显示全部楼层 |阅读模式
这几天我调试代码的时候,老是提示我cout啊,cerr啊,undeclared,
害我调试工作停下来了,不知道是不是我的g++出现问题了?
发表于 2003-9-27 00:17:29 | 显示全部楼层
看看C++标准,
#include <iostream>
using namespace std;
...
 楼主| 发表于 2003-9-27 15:47:10 | 显示全部楼层

  1. #include <iostream>
  2. using namespace std;

  3. template<class T>
  4. T max(T a,T b)
  5. {
  6.    return a>b?a:b;
  7. }

  8. int main()
  9. {
  10. cout<<"Max(3,5) is"
  11.      <<max(3,5)<<endl;
  12. cout<<"Max('3','5') is"
  13.      <<max('3','5')<<endl;
  14. }
复制代码

就是这个模块,
错误提示:

  1. tmp.cpp: In function `int main()':
  2. tmp.cpp:12: call of overloaded `max(int, int)' is ambiguous
  3. tmp.cpp:6: candidates are: T max(T, T) [with T = int]
  4. /usr/include/c++/3.2.2/bits/stl_algobase.h:169:                 const _Tp&
  5.    std::max(const _Tp&, const _Tp&) [with _Tp = int]
  6. tmp.cpp:13: call of overloaded `max(char, char)' is ambiguous
  7. tmp.cpp:6: candidates are: T max(T, T) [with T = char]
  8. /usr/include/c++/3.2.2/bits/stl_algobase.h:169:                 const _Tp&
  9.    std::max(const _Tp&, const _Tp&) [with _Tp = char]
复制代码
发表于 2003-9-27 16:44:34 | 显示全部楼层
max是std空间的名字,所以冲突了,
1. 改个名字,比如my_max之类的
2. 不要引入整个std,把using namespace std;改成
using std::cout;
using std::endl;
发表于 2003-9-27 21:16:19 | 显示全部楼层
斑竹果然厉害
 楼主| 发表于 2003-9-28 15:06:20 | 显示全部楼层
谢谢!能提供点using namespace std这方面的资料吗?
发表于 2003-9-28 19:29:49 | 显示全部楼层
顶上的FAQ写了
不是很多
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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