首頁  >  文章  >  後端開發  >  為什麼我不能在 C 中“cout”我的字串?

為什麼我不能在 C 中“cout”我的字串?

Susan Sarandon
Susan Sarandon原創
2024-10-27 12:20:02320瀏覽

Why Can't I `cout` My Strings in C  ?

輸出字串的困境

在 C 領域,嘗試使用「cout」顯示字串有時會導致令人困惑的錯誤。考慮這個令人費解的場景:

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

遇到這個神秘訊息的程式設計師有禍了:

error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion)

即使是最簡單的字串輸出,例如:

<code class="cpp">string text;
text = "hello";
cout << "String is : " << text << endl;

似乎遇到阻力。

增強你的編譯能力

為了克服這個困境,必須召喚兩個忠實的同伴:

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

透過調用這些標頭,您可以賦予程式碼輕鬆駕馭複雜水域的能力。有這些戰友在身邊,你可以再次享受「cout」的樂趣,告別那些惱人的錯誤。

以上是為什麼我不能在 C 中“cout”我的字串?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

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