vs2005下的Error C2446
VS2005下的 cannot convert parameter 1 from 'const char [5]' to 'LPCTSTR'错误解决
FAQ: Cannot convert from 'const char [..]' to 'LPCTSTR'
Question
I'm trying to compile a piece of code such as:
MessageBox("Hello world!");
... when I compile the project, the compiler yields:
error C2664: 'CWnd::MessageBoxW' : cannot convert parameter 1 from 'const char [12]' to 'LPCTSTR'
What am I doing wrong?
Problem
This error message means that you are trying to pass a multi-byte string (const char [12]) to a function which expects a unicode string (LPCTSTR). The LPCTSTR type extends to const TCHAR*, where TCHAR is char when you compile for multi-byte and wchar_t for unicode. Since the compiler doesn't accept the char array, we can safely assume that the actual type of TCHAR, in this compilation, is wchar_t.
Resolution
You will have to do one of two things:
1. Change your project configuration to use multibyte strings. Press ALT+F7 to open the properties, and navigate to Configuration Properties > General. Switch Character Set to "Use Multi-Byte Character Set".
.NET中对应为:项目->属性页 ->配置属性->常规->项目默认值:字符集
2. Indicate that the string literal, in this case "Hello world!" is of a specific encoding. This can be done through either prefixing it with L, such as L"Hello world!", or surrounding it with the generic _T("Hello world!") macro. The latter will expand to the L prefix if you are compiling for unicode (see #1), and nothing (indicating multi-byte) otherwise.
Variations
在使用VS2005编译代码时遇到Error C2446错误,意味着尝试将一个多字节字符串传递给期望Unicode字符串的函数。解决方法包括修改项目配置以使用多字节字符串或明确指定字符串的编码。可以通过设置项目属性为使用多字节字符集,或者通过在字符串前添加L前缀或使用generic_T宏来指示特定编码。
1237

被折叠的 条评论
为什么被折叠?



