""与<>引起的编译错误,虽然排除,但不明白为什么!

keiran 2003-07-09 10:07:07
这是我用引号""包含文件的源程序:
#include "stdafx.h"
#include "iostream.h"
#include "string.h"

//using namespace std;
using namespace std;

//using namespace std;

void main()
{
string s1;
string s2="ABCDEFGHIJKLMN";
string s3=s2;
string s4(20,'A');
string s5(s2,3,3);
cout<<"s1:"<<s1<<endl;
cout<<"s2:"<<s2<<endl;
cout<<"s3:"<<s3<<endl;
cout<<"s4:"<<s4<<endl;
cout<<"s5:"<<s5<<endl;
}

编译的时候竟然蹦出13个错误!!我将包含文件的方式改为尖括号<>后代码如下:
#include "stdafx.h"
#include <iostream> //========================>>改动处
#include <string> //========================>>改动处

//using namespace std;
using namespace std;

//using namespace std;

void main()
{
string s1;
string s2="ABCDEFGHIJKLMN";
string s3=s2;
string s4(20,'A');
string s5(s2,3,3);
cout<<"s1:"<<s1<<endl;
cout<<"s2:"<<s2<<endl;
cout<<"s3:"<<s3<<endl;
cout<<"s4:"<<s4<<endl;
cout<<"s5:"<<s5<<endl;
}
编译顺利通过,运行正常,但我不明白为什么,请高手指点,谢谢!
...全文
61 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
magic007 2003-07-09
  • 打赏
  • 举报
回复
首先说说<>和""
<>是在编译时指定的include目录以及INCLUDE环境变量指定的目录中查找文件,
而""在该include语句的文件的当前目录中查找,还在编译指定的include目录中及INCLUDE环境变量指定的目录中查找文件。
楼主所说的情况,其实跟""和<>无关
标准C++规定,无.h扩展名的头文件是标准C++头文件,使用了namespace
而其他的有.h扩展名的头文件是为了和以前的C++保持兼容的头文件,没有使用namespace
这就不难理解楼主所说的编译错误了。
将#include <iostream>换成#include "iostream"也一样能通过编译。
而用#include "iostream.h"则需要去掉using namespace std;因为在iostream.h中没有std namespace
penc 2003-07-09
  • 打赏
  • 举报
回复
mark;
MaiCle 2003-07-09
  • 打赏
  • 举报
回复
<>一般是引入C++标准库时使用,其查找方法同 glasswing(蜻蜓飞飞) 所说。
wolfliu 2003-07-09
  • 打赏
  • 举报
回复
如果你使用 iostream.h 和 string.h 就不用使用 using namespace std
只有你使用 iostream and string 的时候才使用 using namespace std

glasswing 2003-07-09
  • 打赏
  • 举报
回复
预处理指示符#include后面跟的文件名有两种格式<>和""
区别在于:<filename>中的文件名,编译器将在标准头文件目录下寻找;"filename"的文件在当前路径下查找这个文件
晨星 2003-07-09
  • 打赏
  • 举报
回复
http://expert.csdn.net/Expert/FAQ/FAQ_Index.asp?id=169251

69,373

社区成员

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

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