链接: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。