7

次の行でコンパイル エラーが発生します。

 MessageBox(e.getAllExceptionStr().c_str(), _T("Error initializing the sound player"));

Error   4   error C2664: 'CWnd::MessageBoxA' : cannot convert parameter 1 from 'const wchar_t *' to 'LPCTSTR'   c:\users\daniel\documents\visual studio 2012\projects\mytest1\mytest1\main1.cpp 141 1   MyTest1

このエラーを解決する方法がわかりません。次のことを試しました。

MessageBox((wchar_t *)(e.getAllExceptionStr().c_str()), _T("Error initializing the sound player"));
MessageBox(_T(e.getAllExceptionStr().c_str()), _T("Error initializing the sound player"));

「マルチバイト文字セットを使用する」設定を使用していますが、変更したくありません。

4

4 に答える 4

5

最も簡単な方法は、MessageBoxWの代わりに使用することですMessageBox

MessageBoxW(e.getAllExceptionStr().c_str(), L"Error initializing the sound player");

2 番目に簡単な方法は、元の CString から新しい CString を作成することです。必要に応じて、ワイド文字列と MBCS 文字列との間で自動的に変換されます。

CString msg = e.getAllExceptionStr().c_str();
MessageBox(msg, _T("Error initializing the sound player"));
于 2015-03-09T16:18:38.240 に答える
1

LPCSTR = const char*. これはconst wchar*明らかに同じものではありません。

API 関数に正しいパラメーターを渡していることを常に確認してください。_T("")タイプ C-string はワイド文字列であり、そのバージョンの では使用できませんMessageBox()

于 2015-03-09T16:19:42.743 に答える
1

e.getAllExceptionStr().c_str()ワイド文字列を返す場合、次のように動作します。

MessageBoxW(e.getAllExceptionStr().c_str(), L"Error initializing the sound player");

;Wの末尾に注意してください。MessageBoxW

于 2015-03-09T16:19:49.383 に答える
0

古い MBCS モードでコンパイルする場合は、次のようなATL/MFC 文字列変換ヘルパーCW2Tを使用することをお勧めします。

MessageBox(
    CW2T(e.getAllExceptionStr().c_str()),
    _T("Error initializing the sound player")
);

あなたのgetAllExceptionStr()メソッドは a を返すようですstd::wstringので、それを呼び出すとa.c_str()が返されますconst wchar_t*

CW2T-stringからwchar_t-string に変換TCHARします。これは、あなたの場合 (MBCS コンパイル モードを考慮して)、-string と同等charです。

ただし、Unicode ( wchar_t-strings) から MBCS ( char-strings) への変換は損失を伴う可能性があることに注意してください。

于 2015-03-09T16:19:33.813 に答える