头文件之惑 C++系列(2)(2006-11-21 10:16:17)
来源:互联网 发布:mac 优酷升级后 播放 编辑:程序博客网 时间:2024/06/05 08:22
===============
C++学习指导系列文章(2)
===============
***为了帮助大家学习C++,所以着手撰写系列文章,希望对大家有所帮助。有不当之处,也希望大家提建议。
***Bigleo 2006.11
头文件之惑
安装好VC6后,在inlude目录中,会看下列文件:
iostream
iostream.h
string
string.h
...
iostream.h
string
string.h
...
这些头文件,大多是成对出现。使用这些文件时,有的C++书(如钱能的C++程设计教程)中用有.h的头文件,有的C++书(如Nell dale的Programming in C++)中用没有.h的头文件。这是为什么?有.h的头文件与没有.h有头文件有什么区别?
有.h的头文件是为了与C更好的兼容,这是C++的目标之一:尽可能的包容C。其中的string、cin、cout等是以全局变量的方式定义的。C++1998-9-1标准如是讲:The ".h" headers dump all their names into the global namespace, whereas the newer forms keep their names in namespace std. Therefore, the newer forms are the preferred forms for all uses except for C++ programs which are intended to be strictly compatible with C.
无.h的头文件采用了名称空间技术,把cin、cout、string等放到了标准名字空间std中。在使用此类头文件时,如果要使用其中的内容,要指定标准名字空间。微软的文档中如是讲:
The ANSI/ISO C++ standard requires you to explicitly(显示地) declare the namespace in the standard library. For example, when using iostream, you must specify the namespace of cout in one of the following ways:
std::cout (explicitly)
using std::cout (using declaration)
using namespace std (using directive)
The ANSI/ISO C++ standard requires you to explicitly(显示地) declare the namespace in the standard library. For example, when using iostream, you must specify the namespace of cout in one of the following ways:
std::cout (explicitly)
using std::cout (using declaration)
using namespace std (using directive)
看一下下面这个例子所有的,会有什么错误:
#include
#include
#include
int main(){
string st;
st = "";
}
#include
#include
int main(){
}
对,代码没有通过编译。因为在上面的代码中名字空间std 的成员不能被不加限定修饰地访问。为了修正这个错误我们可以选择下列方案之一:
用适当的限定修饰名代替例子中的名字空间std 成员的名字
用using 声明使例子中用到的名字空间std 的成员可见
用using 指示符使来自名字空间std 的全部成员可见
如改为如下代码,即可正确编译:
#include
#include
#include
using namespace std;
int main(){
string st;
st = "";
}
用适当的限定修饰名代替例子中的名字空间std 成员的名字
用using 声明使例子中用到的名字空间std 的成员可见
用using 指示符使来自名字空间std 的全部成员可见
如改为如下代码,即可正确编译:
#include
#include
#include
using namespace std;
int main(){
}
关于标准名称空间std,有些较早的C++书中称其会自动包含到程序中去,有些开发工具也是这样做的,比如说C++编译器gccgcc 2.9.x会自动加using namespace std。在 C++ ISO 1998-9-1标准要求显示的使用std,如GCC3.0.12,VC6,C++ Builder6都是这样。
- 头文件之惑 C++系列(2)(2006-11-21 10:16:17)
- C语言笔记之头文件与链接(一)
- C语言笔记之头文件与链接(二)
- c语言一起学习之建立头文件(二)
- C语言头文件之float.h
- C常见问题之头文件引用
- C语言头文件之ctype.h
- c语言之头文件的使用
- C语言之头文件包含问题
- C 头文件之<ctpye.h>
- C预言之头文件路径选择
- C语言之头文件包含问题
- [C语言]头文件之stdarg
- C/C++笔试必须熟悉掌握的头文件系列(五)——iostream
- C/C++笔试必须熟悉掌握的头文件系列(六)——sstream
- C/C++笔试必须熟悉掌握的头文件系列(八)——algorithm
- C/C++笔试必须熟悉掌握的头文件系列(九)——string
- java文件处理 之 读写TXT(比之c++,重置文件头,int转string)
- C++系列文章 main函数(2007-04-24 15:33:04)
- Android权限列表permission说明
- v4l2 spec 中文 Ch03
- 在VC2005(Express版)中如何创建标准C++程序
- Hibernate断网后不能解析cfg配置文件,连接失败的问题
- 头文件之惑 C++系列(2)(2006-11-21 10:16:17)
- 机器学习 搜索引擎 数据挖掘 好的网址
- 从小函数实现看应聘者的编程素质(atoi.strcmp...)
- C++开发工具 C++系列(1)(2006-11-20 09:50:25)
- SOFTWARE
- Sharing Files between RHEL 6 and Windows Systems with Samba
- shell 遍历目录下的文件
- 数据库优化设计方案
- 软件开发工具必会(2007-09-10 15:25:24)