ホームページ  >  記事  >  バックエンド開発  >  「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 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。