[英]c++ variable declaration syntax
当我做这样简单的事情时:
char text1[] = "hello world";
MessageBox(NULL, text1, NULL, NULL);
我收到此错误:
Error 1 error C2664: 'MessageBoxW' : cannot convert parameter 2 from 'char [12]' to 'LPCWSTR'
您有两个基本问题。 首先,一个char
只能容纳一个字符,不能容纳一串字符。 其次,您有一个“窄”字符串文字,但是(显然)您使用的是应用程序的Unicode构建,其中MessageBox
期望接收宽字符串。 您想要:
wchar_t text1[] = L"hello world";
要么:
wchar_t const *text1 = L"hello world";
或(最常见):
std::wstring text1(L"hello world");
...但是请注意,不能将std::wstring
直接传递给Messagebox
。 您需要在调用MessageBox
时传递text1.c_str()
,或者为MessageBox
编写一个小的包装器,该包装器接受(引用到) std::wstring
,类似于:
void message_box(std::wstring const &msg) {
MessageBox(NULL, msg.c_str(), NULL, MB_OK);
}
char
是single character
,而不是String。
您需要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";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.