cannot convert parameter 2 from 'const char [12]' to 'LPCWSTR'
来源:互联网 发布:淘宝店铺联盟怎么开通 编辑:程序博客网 时间:2024/05/15 22:51
QuestionI'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".
<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
Another error message, indicating the same problem, would be:
cannot convert parameter 1 from 'const char [12]' to 'LPCWSTR'
Where LPCWSTR maps to a wchar_t pointer, regardless of your build configuration. This problem can be resolved primarily by using solution #2, but in some cases also #1. A lot of the Microsoft provided libraries, such as the Platform SDK, have got two variations of each function which takes strings as parameters. In case of a unicode build, the actual functions are postfixed W, such as the MessageBoxW seen above. In case of multi-byte, the function would be MessageBoxA (ASCII). Which of these functions is actually used when you compile your application, depends on the setting described in resolution #1 above.
0 0
- cannot convert parameter 2 from 'const char [12]' to 'LPCWSTR'
- cannot convert parameter 1 from 'const char *' to 'LPCWSTR
- error C2664: 'MessageBoxW' : cannot convert parameter 2 from 'const char [2]' to 'LPCWSTR'
- 关于VS2005的error C2664: cannot convert parameter 2 from 'const char [2]' to 'LPCWSTR
- error C2664: 'CreateWindowExW' : cannot convert parameter 2 from 'const char [16]' to 'LPCWSTR'
- VS2008中报错:cannot convert parameter 2 from 'char *' to 'LPCWSTR'
- VS2005中报错:cannot convert parameter 2 from 'char *' to 'LPCWSTR'
- VC错误之_cannot convert parameter 2 from 'const char [12]' to 'LPCWSTR'
- error C2664: 'TextOutW' : cannot convert parameter 4 from 'const char [5]' to 'LPCWSTR'
- 'CreateFileW' : cannot convert parameter 1 from 'const char [13]' to 'LPCWSTR'
- 'LoadLibraryW' : cannot convert parameter 1 from 'const char *' to 'LPCWSTR' 解决方案
- 'LoadLibraryW' : cannot convert parameter 1 from 'const char *' to 'LPCWSTR' 解决方案
- 'CreateFileW' : cannot convert parameter 1 from 'const char [13]' to 'LPCWSTR'
- error C2664: 'DeleteFileW' : cannot convert parameter 1 from 'const char *' to 'LPCWSTR'
- C++编译遇到参数错误(cannot convert parameter * from 'const char [**]' to 'LPCWSTR')
- C++编译遇到参数错误(cannot convert parameter * from 'const char [**]' to 'LPCWSTR')
- cannot convert parameter 1 from 'char *' to 'LPCWSTR'
- VS2010 cannot convert parameter 1 from 'char *' to 'LPCWSTR'
- 1>MSVCRTD.lib(MSVCR100D.dll) : error LNK2005: _strncmp 已经在 libcmtd.lib(strncmp.obj) 中定义
- MFC透明位图(2)--PNG的实现
- LeetCode: Minimum Window Substring
- Linux/Unix下pid文件作用浅析
- canvas基于页面宽度绘制饼图数据(适用与多屏幕适配开发)
- cannot convert parameter 2 from 'const char [12]' to 'LPCWSTR'
- 关于ansys里面的谐分析和瞬态分析结果的讨论
- c++自学 关于构造函数的基础
- Best Time to Buy and Sell Stock
- Java 创建Maven Web项目
- SQL Server存储过程 对数组参数的循环处理
- 内存填零杀进程
- 发生的和奋斗史佛挡杀佛多数房
- JavaScript语法支持严格模式:”use strict