当我采取这样简单的事情时:
char text1[] = "hello world";
MessageBox(NULL, text1, NULL, NULL);
我收到此错误:
Error 1 error C2664: 'MessageBoxW' : cannot convert parameter 2 from 'char [12]' to 'LPCWSTR'
当我采取这样简单的事情时:
char text1[] = "hello world";
MessageBox(NULL, text1, NULL, NULL);
我收到此错误:
Error 1 error C2664: 'MessageBoxW' : cannot convert parameter 2 from 'char [12]' to 'LPCWSTR'
你有两个基本问题。首先,achar只能容纳一个字符,不能容纳一串字符。其次,您有一个“窄”字符串文字,但您(显然)正在使用您的应用程序的 Unicode 构建,其中MessageBox期望接收一个宽字符串。你想要:
wchar_t text1[] = L"hello world";
或者:
wchar_t const *text1 = L"hello world";
或(最常见的):
std::wstring text1(L"hello world");
...但请注意 anstd::wstring不能直接传递给Messagebox. 您需要text1.c_str()在调用时通过MessageBox,或者为MessageBox接受的 a (引用) a编写一个小包装器std::wstring,例如:
void message_box(std::wstring const &msg) {
MessageBox(NULL, msg.c_str(), NULL, MB_OK);
}
char是一个single character,而不是一个字符串。
你需要Unicode,可以使用TCHAR;
TCHAR[] text = _T("Hello World.");
MessageBox(NULL, text, NULL, NULL);
C/C++ 中的字符串文字不是值char的集合,而是char值的集合。声明这一点的惯用方式是
const char* text1 = "hello world";
char只保存一个字符,而不是一组字符。
因此,只需使用指向 Unicode 字符常量字符串的指针。
LPCWSTR text1 = L"hello world";