連結:http://blog.csdn.net/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。