初学C++:编译器报错“Cannot open include file: 'iostream.h': No such file or directory”的解决方法

初学C++,使用vs2005写了按照教程写了一个简单的测试程序,在编译的时候报错“Cannot open include file: 'iostream.h': No such file or directory”。

 

旧的源文件如下:

  1. #include "MyArea.h"   
  2. #include <iostream.h>   
  3.   
  4. void main(){  
  5.     double width,length;  
  6.     cout<<"please enter tow number: "<<endl;  
  7.     cin>>width>>length;  
  8.   
  9.     cout<<"area of rectangle is "<<rect(width,length)<<endl;  
  10.       
  11.     double radius;  
  12.   
  13.     cout<<"please enter radius: "<<endl;  
  14.     cin>>radius;  
  15.   
  16.     cout<<"area of circle is "<<circle(radius)<<endl;  
  17.   
  18. }  

 

 

一直报上面的错误,无法通过编译。遂放狗开始搜索。。。。。

得到如下的原因和解决方法:

-----------------------------------------

#include <iostream.h > 是VC6以前的写法。

#include <iostream >
using  namespace  std;

这个是标准库的写法。标准库把这些个文件都放到std这个namespace里面了。

可以到VC/include看看和VC6.0的区别,是iostream而不是iostream.h。

注意 <iostream>和 <iostream.h>是两个不同的东西
<iostream>是STL库
<iostream.h>是兼容于c的库
所有STL库都在std::名空间下
std::cout是 <iostream>里面的对象

namespace std: 所有的C++ Standard Library Class都包含在这个叫std的name
space里。比如 <vector>, <iostream>, <iterator>等等。所以当你使用它们其中的class时
,需要加入这个语句,using namespace std; 不然编译器报错。

 

--------------------------------

 

按照上述的方法修改,果不其然,通过了编译。感谢提供上述解答的无名网友。

 

修改后的代码如下:

 

  1. #include "MyArea.h"   
  2. #include <iostream>   
  3. using namespace std;  
  4.   
  5. void main(){  
  6.     double width,length;  
  7.     cout<<"please enter tow number: "<<endl;  
  8.     cin>>width>>length;  
  9.   
  10.     cout<<"area of rectangle is "<<rect(width,length)<<endl;  
  11.       
  12.     double radius;  
  13.   
  14.     cout<<"please enter radius: "<<endl;  
  15.     cin>>radius;  
  16.   
  17.     cout<<"area of circle is "<<circle(radius)<<endl;  
  18.   
  19. }  

 

知道了原因就很简单了。

 

 

说明:注意所包含的头文件,头文件中的内容也要做相应的修改

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值