请教:#include 和 using namespace std;的顺序问题

wpx 2003-02-04 06:01:55
请看下面的代码,不必看具体的函数定义部分,
主要看#include 和 using namespace std;的顺序,即main.cpp中的(A)处。

环境是VC6.0,以下是编译正常的程序。
///////// hd.h ////////////////////////////////////////
//声明两个函数
inline const vector<int>* fibon_seq(int size);
inline bool fibon_elem(int pos,int &elem,const std::vector<int>* (*seq_ptr)(int));
///////////////////////////////////////////////////////

/////// function.cpp /////////////////////////////////
//定义两个函数
#include<iostream>
#include<vector>
using namespace std;

const vector<int>* fibon_seq(int size)
{
//......
}

bool fibon_elem(int pos,int &elem,const vector<int>* (*seq_ptr)(int))
{
const vector<int> *pseq=fibon_seq(pos);
elem=(*pseq)[pos-1];
return true;
}
////////////////////////////////////////////////////

////// main.cpp ////////////////////////////////////
//主程序
#include<iostream>
#include<vector>
// 注意!--------------------->(A)
using namespace std;
#include"hd.h"

int main()
{
int elem=0,pos=0;
const vector<int>* (*seq_ptr)(int)=fibon_seq;

cout<<"Input Pos:";
cin >>pos;

if(!fibon_elem(pos,elem,seq_ptr))
cout<<"internal error!";
else
cout<<"is:" << elem;

return 0;
}
/////////////////////////////////////////////////////
在(A)处,如果这里把“#include"hd.h" ”写在"using namespace std;"的前一行,就会编译出类似下面的错误:

d:\test25\hd.h(3) : error C2143: syntax error : missing ';' before '<'
d:\test25\hd.h(3) : error C2433: 'vector' : 'inline' not permitted on data declarations
d:\test25\hd.h(3) : error C2734: 'vector' : const object must be initialized if not extern
d:\test25\hd.h(3) : error C2143: syntax error : missing ';' before '<'
D:\test25\main.cpp(14) : error C2872: 'vector' : ambiguous symbol
D:\test25\main.cpp(14) : error C2143: syntax error : missing ';' before '<'
D:\test25\main.cpp(14) : error C2734: 'vector' : const object must be initialized if not extern
D:\test25\main.cpp(14) : error C2143: syntax error : missing ';' before '<'
D:\test25\main.cpp(16) : error C2065: 'seq_ptr' : undeclared identifier
D:\test25\main.cpp(16) : error C2065: 'fibon_seq' : undeclared identifier

为什么会这样呢?难道 “using namespace std;”必须写在“#include"hd.h" 之前吗?
...全文
602 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
rivershan 2003-02-04
  • 打赏
  • 举报
回复
#include 一般都是放在最前面的~
然后是
using
然后是函数声明一类~
merlinran 2003-02-04
  • 打赏
  • 举报
回复
其实在这里根本不应该用using directive。把整个namespace全部引入全局namespace,和定义全局的变量和函数没有区别。

在此还是用using declaration(using std::vector...)或者直接加上名字标识(std::vector<int>...)。

在大多数教材上,都是直接用using namespace std;来引入全部的符号,这只是为简单起见,在实际应用中并不能合适。
danmao 2003-02-04
  • 打赏
  • 举报
回复
如果你把hd.h中间改动一下,结果就不一样了:
inline const vector<int>* fibon_seq(int size);
inline bool fibon_elem(int pos,int &elem,const vector<int>* (*seq_ptr)(int));
注意:vector模板需要使用命名空间,所以你把using namespace std;放到hd.h的最前面。
// 注意!--------------------->(A)处的顺序就无所谓了。
杨冬青 2003-02-04
  • 打赏
  • 举报
回复
vector是属于std空间空间的,必须显示声明。在头文件里使用using不是好风格,也不建议在头文件之前使using,比较好的做法是使用名字空间限定符如下:

///////// hd.h ////////////////////////////////////////
//声明两个函数
inline const std::vector<int>* fibon_seq(int size);
inline bool fibon_elem(int pos,int &elem,const std::vector<int>* (*seq_ptr)(int));
///////////////////////////////////////////////////////
Caoyu015 2003-02-04
  • 打赏
  • 举报
回复
的确是要将using namespace std,写hd.h之前,因为你用的容器类是在std
名字空间声明的。
saucer 2003-02-04
  • 打赏
  • 举报
回复
change your hd.h to:

#include<vector>
using namespace std;

inline const vector<int>* fibon_seq(int size);
inline bool fibon_elem(int pos,int &elem,const std::vector<int>* (*seq_ptr)(int));

magicblue 2003-02-04
  • 打赏
  • 举报
回复
楼上的hollies(冬青) 和 merlinran(天行者) 说的很清楚了
补充一下:这在《More Exceptional C++》中最后几个ITEM里也说得比较清楚。
Frank001 2003-02-04
  • 打赏
  • 举报
回复
楼上的各位都说的很清楚了。
我想应该是这样理解:
把using namespace std;放在#include"hd.h"的前面,等于说hd.h里面也可以引用using namespace std; 因此编译就通过了。
如果把using namespace std;放在#include"hd.h"的后面,就不能引用了,加上hd.h里面少了这句using namespace std;所以就会出现那些编译错误。
解决的方法一个你已经想到了,
还有就是在"hd.h"里面也写入using namespace std;
再有就是直接加上名字标识std::,这个方法相对比较的安全。
Frank001 2003-02-04
  • 打赏
  • 举报
回复
楼上的各位都说的很清楚了。
我想应该是这样理解:
把using namespace std;放在#include"hd.h"的前面,等于说hd.h里面也可以引用using namespace std; 因此

编译就通过了。
如果把using namespace std;放在#include"hd.h"的后面,就不能引用了,加上hd.h里面少了这句using

namespace std;所以就会出现那些编译错误。
解决的方法一个你已经想到了,
还有就是在"hd.h"里面也写入using namespace std;
再有就是直接加上名字标识std::,这个方法相对比较的安全。

70,029

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧