error C2679: binary ‘

來源:互聯網
上載者:User

連結: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。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.