首頁  >  文章  >  後端開發  >  為什麼我不能使用 `cout` 輸出字串?

為什麼我不能使用 `cout` 輸出字串?

Patricia Arquette
Patricia Arquette原創
2024-10-27 08:03:30564瀏覽

Why can't I output strings using `cout`?

解決cout無法輸出字串的問題

為什麼cout無法輸出字串?讓我們深入研究這個問題並提供解決方案。

在您的程式碼片段中:

<code class="cpp">string text;
text = WordList[i].substr(0, 20);
cout << "String is  : " << text << endl;

您將遇到以下錯誤:

Error 2   error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion)  c:\users\mollasadra\documents\visual studio 2008\projects\barnamec\barnamec\barnamec.cpp    67  barnamec**

解決此錯誤,您必須包含必要的標頭:

<code class="cpp">#include <string>
#include <iostream></code>

標頭包含std::string 類別的聲明,而 則包含std::string 類別的聲明。標頭包含 std::cout 物件的定義和

這是修正後的程式碼:

<code class="cpp">#include <string>
#include <iostream>

string text;
text = WordList[i].substr(0, 20);
cout << "String is  : " << text << endl;</code>

現在,您應該可以使用 cout 成功輸出字串。

以上是為什麼我不能使用 `cout` 輸出字串?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn