0

こんにちは、Visual Studio 2008 でこのコードをコンパイルすると、次のエラーが発生します。

#include<iostream>
#include<string>
using namespace std;
void main()
{
     basic_string<wchar_t> abc("hello world");
     cout<<abc;
     return;
}

エラー C2664: 'std::basic_string<_Elem,_Traits,_Ax>::basic_string(std::basic_string<_Elem,_Traits,_Ax>::_Has_debug_it)': パラメーター 1 を 'const char [12]' から ' に変換できませんstd::basic_string<_Elem,_Traits,_Ax>::_Has_debug_it'

エラー C2679: バイナリ '<<' : 'std::basic_string<_Elem,_Traits,_Ax>' 型の右側のオペランドを取る演算子が見つかりません (または、受け入れ可能な変換がありません)

私が間違っているのは何ですか?

背後で起こっていることを理解するのを手伝ってくれる人はいますか? ありがとう

4

3 に答える 3

3

試す:

エラー C2664:

basic_string<wchar_t> abc(L"hello world");

エラー C2679:

cout << abc.c_str();

(コンパイラは、ユーザーが作成したすべてのタイプに適切なオーバーロードを提供できない/提供しないため。ただし、これは標準タイプでもあるwstringため、適切なヘッダーを調べたところ、 aまたは a のoperator<<いずれかを取る適切なものが見つかりませんでした。)stringwstring

を使用するint mainと、次のようになります。

int main(void)
{        
     basic_string<wchar_t> abc(L"hello world");
     cout << abc.c_str() << endl;
     return 0;
}

std::wstringただし、車輪を再発明するのではなく、実際に使用する必要があります。

于 2009-04-02T11:22:38.173 に答える
3

wchar_t は、ワイド文字タイプを指定します。デフォルトでは、リテラル文字列への const char ポインターはワイドではありませんが、「L」を前に付けることで、ワイド文字配列として扱うようにコンパイラーに指示できます。

だからちょうどに変更します

basic_string<wchar_t> abc(L"hello world");
于 2009-04-02T11:28:24.727 に答える
2

問題は、ワイド文字と (ナロー?) 文字タイプを混在させていることです。

にはbasic_string、次のいずれかを使用します。

// note the L"..." to make the literal wchar_t
basic_string<wchar_t> abc(L"hello world");  

// note that basic_string is no longer wchar_t
basic_string<char> abc("hello world");

または同等のもの:

// wstring is just a typedef for basic_string<wchar_t>
wstring abc(L"hello world");

// string is just a typedef for basic_string<char>
string abc("hello world");

出力も一致するように変更します。

cout << abc;   // if abc is basic_string<char>

wcout << abc;  // if abc is basic_string<wchar_t>
于 2009-04-03T17:17:53.420 に答える