CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
可用分押宝游戏火热进行中... 专题改版:Java Web 专题
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  C/C++ >  C语言

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

楼主wpx(wpx)2003-02-04 06:01:55 在 C/C++ / C语言 提问

请看下面的代码,不必看具体的函数定义部分,  
  主要看#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"   之前吗?  
  问题点数:100、回复次数:9Top

1 楼saucer(思归)回复于 2003-02-04 06:05:45 得分 20

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));  
   
  Top

2 楼Caoyu015(酷鱼一代)回复于 2003-02-04 07:11:46 得分 0

的确是要将using   namespace   std,写hd.h之前,因为你用的容器类是在std  
  名字空间声明的。Top

3 楼hollies(冬青)回复于 2003-02-04 08:38:10 得分 30

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));  
  ///////////////////////////////////////////////////////  
  Top

4 楼danmao(愤怒的小mao)回复于 2003-02-04 12:08:49 得分 0

如果你把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)处的顺序就无所谓了。Top

5 楼merlinran(天行者)回复于 2003-02-04 12:28:18 得分 0

其实在这里根本不应该用using   directive。把整个namespace全部引入全局namespace,和定义全局的变量和函数没有区别。  
   
  在此还是用using   declaration(using   std::vector...)或者直接加上名字标识(std::vector<int>...)。  
   
  在大多数教材上,都是直接用using   namespace   std;来引入全部的符号,这只是为简单起见,在实际应用中并不能合适。Top

6 楼rivershan(阿门)回复于 2003-02-04 12:52:57 得分 0

#include   一般都是放在最前面的~  
  然后是  
  using    
  然后是函数声明一类~Top

7 楼Frank001(Frank)回复于 2003-02-04 13:55:45 得分 50

楼上的各位都说的很清楚了。  
  我想应该是这样理解:  
  把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::,这个方法相对比较的安全。Top

8 楼Frank001(Frank)回复于 2003-02-04 13:58:33 得分 0

楼上的各位都说的很清楚了。  
  我想应该是这样理解:  
  把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::,这个方法相对比较的安全。  
  Top

9 楼magicblue(小飞侠)回复于 2003-02-04 14:46:27 得分 0

楼上的hollies(冬青)   和   merlinran(天行者)   说的很清楚了  
  补充一下:这在《More   Exceptional   C++》中最后几个ITEM里也说得比较清楚。Top

相关问题

  • 讨论:#include<iostream> using namespace std; & #include <iostream.h>
  • 关于#include<iostream> using namespace std;高手进
  • about "using namespace std"
  • using namespace std??在线!
  • 关于 using namespace std
  • 怎样才能让vc6.0下也能用 #include <iostream> using namespace std; ?
  • 奇怪的问题,挑战高手,#include "iostream.h"和#include <iostream> using namespace std不一样
  • 关于std::__Fiopen 和using namespace
  • 菜鸟问关于namespace std
  • using namespace std;说明已经引用了std命名空间,为什么还用#include包含<iostream>呢?

关键词

  • 函数
  • vector
  • 编译
  • cpp
  • fibon
  • hd
  • seq
  • using namespace std
  • 顺序
  • std

得分解答快速导航

  • 帖主:wpx
  • saucer
  • hollies
  • Frank001

相关链接

  • C/C++ Blog
  • C/C++类图书
  • C/C++类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
世纪乐知(北京)网络技术有限公司 版权所有, 京 ICP 证 020026 号
北京创新乐知广告有限公司 提供技术支持
Copyright © 2000-2007, CSDN.NET, All Rights Reserved
GongshangLogo