首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

向,请问一下有关头文件的用法

2012-03-02 
向各位高手,请教一下有关头文件的用法。#include iostream#include math.h usingnamespacestdvoidmain

向各位高手,请教一下有关头文件的用法。
#include <iostream>
#include "math.h "    
using   namespace   std;
void   main()
{
const   double   pai=3.14159,a=16.0;
const   int   aspect=2;
double   angle,p;
int   x,y;
char   rose[25][80];
for(x=0;x <80;x++)
for(y=0;y <25;y++)
rose[y][x]= '   ';
for(int   i=0;i <128;i++)
{
angle=i*pai/64;
p=a*sin(2*angle);
x=int(p*cos(angle))*aspect+40;
y=int(p*sin(angle))+13;
rose[y][x]= '* ';
}
for(y=0;y <25;y++)
{
for(x=0;x <80;x++)
cout < <rose[y][x];
cout < <endl;
}
}

在上面的程序中,如果头文件改为:
#include <iostream.h>
#include <math.h>
之后,不能再有:using   namespace   std;

各位可以帮我解释一下吗?
谢谢!




[解决办法]
#include <iostream.h> 包含的是原来的C库
#include <iostream> 包含的是C++库
[解决办法]
这种说法不对:#include <iostream.h> 包含的是原来的C库

这还是C++文件,

iostream.h/iostream就是两个不同的头文件
后者在std namespace种定义,前者不是
永远使用iostream,因为iostream.h不是C++最新标准里的东西



[解决办法]
#include <math.h> 编译器只会搜索环境变量中设定的包含目录以寻找相应头文件
#include "math.h "还会搜索当前工作目录
[解决办法]
#include <math.h>
从标准库目录开始搜索文件

#include "math.h "
从当前目录开始搜索文件
[解决办法]
要是我就写
#include <iostream>
#include <cmath>
using namespace std;
[解决办法]
当你自己写个头文件,放在源文件的同一目录下,就用#include "math.h ",搜索快一点

[解决办法]
库引用需要命名空间
头文件引用不需要
[解决办法]
刚开始时C++是从C逐渐演化过来的,而不是重新设计的,所以直到C++标准制定前在头文件上有些混乱。用几个例子说明一下:

如果要在C中操作输入输出和字符串:
#include <stdio.h>
#include <string.h>
其中stdio.h含有printf和scanf, string.h含有strcpy和strcat

标准制定以前在C++中操作输入输出和字符串:
#include <stdio.h>
#include <string.h>
#include <iostream.h>
其中iostream.h含有cout和cin,那时还没有namespace的概念,所以这种写法没有使用using namespace std

标准制定以后在C++中操作输入输出和字符串:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
using namespace std
其中的cstdio和cstring就是C语言的stdio.h, string.h 这样你就可以使用C函数库中的prinf,strcpy之类。依此类推还有cmath, cstdlib等等。

iostream和string代表你要使用C++类库中的cout, string类型以及string的成员函数,这时C++库已经被放在了std中,所以要用using namespace std

除此以外还有另一种写法:
#include <iostream>
#include <string>
...
std::string ...
std::cout ... std::endl;
...

更详细的说明可以参考《effective c++》

热点排行