C++學習——string和string.h__C++

來源:互聯網
上載者:User

一直用c比較習慣,最近開始學C++,一開始就遇到了問題。如下的簡單程式,string的使用。

#include<iostream>#include<string.h>using namespace std;int main(){string str;cin >> str;for (auto c : str)cout << c << endl;}

VS提示錯誤如下: 錯誤 1 error C2679: 二進位“>>”: 沒有找到接受“std::string”類型的右運算元的運算子(或沒有可接受的轉換) c:\users\likai\documents\visual studio 2013\projects\容器\容器\源.cpp 7 1 容器
2 IntelliSense:  沒有與這些運算元匹配的 ">>" 運算子
            運算元類型為:  std::istream >> std::string c:\Users\likai\Documents\Visual Studio 2013\Projects\容器\容器\源.cpp 7 6 容器
仔細查看後,是自己將C中的string,h標頭檔在C++中使用,在C++中,string和string.h檔案可以同時存在,string.h是C中的標頭檔。而在C++中誤認為可行,導致運算子對>>對string不能辨別。所以應在C++中避免包括C標準庫中的.h檔案。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.