error C2679: binary '<<' : no operator defined which takes a right-hand operand of type '' (or there is no acceptable conversion

链接:http://blog.youkuaiyun.com/gengxt2003/archive/2007/06/07/1642236.aspx

 

简单程序

#include <iostream.h>
#include <string>
#include <list>
#include <algorithm>

using namespace std;

PrintIt (string& StringToPrint) {
 std::cout<<StringToPrint<<endl;
//; cout<<"asdfds"<<endl;
}

int main (void) {
  list<string> FruitAndVegetables;
  FruitAndVegetables.push_back("carrot");
 
  for_each  (FruitAndVegetables.begin(), FruitAndVegetables.end(), PrintIt);
}
 

如果 #include <iostream.h> 则有错误,
错误如题所示:

error C2679: binary '<<' : no operator defined which takes a right-hand operand of type '' (or there is no acceptable conversion)

把其中的 “.h”去掉,则错误消失,能够正常运行!

 

==========VeryCD阵亡的分割线=========================

以下是我加的内容,其实是很基础的知识。

 

原因:来自百度百科:http://baike.baidu.com/view/1618473.htm?fr=ala0


<iostream>和<iostream.h>是不一样,前者没有后缀,实际上,在你的编译器include文件夹里面可以看到,二者是两个文件,打开文件就会发现,里面的代码是不一样的。

  后缀为.h的头文件c++标准已经明确提出不支持了,早些的实现将标准库功能定义在全局空间里,声明在带.h后缀的头文件里,c++标准为了和C区别开,也为了正确使用命名空间,规定头文件不使用后缀.h。

  因此,当使用<iostream.h>时,相当于在c中调用库函数,使用的是全局命名空间,也就是早期的c++实现;当使用<iostream>的时候,该头文件没有定义全局命名空间,必须使用namespace std;这样才能正确使用cout。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值