Cannot convert from 'const char [..]' to 'LPCTSTR'
2008-07-05 16:03
531 查看
我是用Visual studio 2005的默认设置,照书上按部就班,不曾定义什么_UNICODE(看了文档,这个好像是16位一个字符的意思吧?方便国际化,呵呵,不太懂)。不过,我的程序只要涉及字符操作,都要加_T()。我知道这个跟_UNICODE有关系。不然一大堆错,多是cannot convert chat* to LPCTSTR之类的。但是书上的程序都没有加_T()的,可能是书的年纪太大的缘故吧,没办法,人家说这本书(VC6.0 技术内幕)好啊。
VS 2005好像默认是定义UNICODE的。
不过你既然养成了用_T()定义字符串的好习惯,最好也用wcsnicmp代替strnicmp
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:
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".
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.
LPCTSTR是什么意思?
如何理解LPCTSTR, L表示long指针, 这是为了兼容Windows 3.1等16位操作系统遗留下来的, 在win32中以及其他的32为操作系统中, long指针和near指针及far修饰符都是为了兼容的作用。没有实际意义。P表示这是一个指针C表示是一个常量T在Win32环境中, 有一个_T宏, 这个宏用来表示你的字符是否使用UNICODE, 如果你的程序定义了UNICODE或者其他相关的宏, 那么这个字符或者字符串将被作为UNICODE字符串, 否则就是标准的ANSI字符串。STR表示这个变量是一个字符串。所以LPCTSTR就表示一个指向常固定地址的可以根据一些宏定义改变语义的字符串。同样, LPCSTR就只能是一个ANSI字符串, 在程序中我们大部分时间要使用带T的类型定义。 LPCTSTR == const TCHAR *
VS 2005好像默认是定义UNICODE的。
不过你既然养成了用_T()定义字符串的好习惯,最好也用wcsnicmp代替strnicmp
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:
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".
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.
LPCTSTR是什么意思?
如何理解LPCTSTR, L表示long指针, 这是为了兼容Windows 3.1等16位操作系统遗留下来的, 在win32中以及其他的32为操作系统中, long指针和near指针及far修饰符都是为了兼容的作用。没有实际意义。P表示这是一个指针C表示是一个常量T在Win32环境中, 有一个_T宏, 这个宏用来表示你的字符是否使用UNICODE, 如果你的程序定义了UNICODE或者其他相关的宏, 那么这个字符或者字符串将被作为UNICODE字符串, 否则就是标准的ANSI字符串。STR表示这个变量是一个字符串。所以LPCTSTR就表示一个指向常固定地址的可以根据一些宏定义改变语义的字符串。同样, LPCSTR就只能是一个ANSI字符串, 在程序中我们大部分时间要使用带T的类型定义。 LPCTSTR == const TCHAR *
相关文章推荐
- error C2664: 'int CListCtrl::InsertColumn(int,LPCTSTR,int,int,int)' : cannot convert parameter 2 from 'const char [7]' to 'LPCTS
- error C2664: 'CWnd::MessageBoxW' : cannot convert parameter 1 from 'const char [12]' to 'LPCTSTR'解决方案
- error C2664: 'CWnd::MessageBoxW' : cannot convert parameter 1 from 'const char [12]' to 'LPCTSTR'解决方案
- VS2005下的 cannot convert parameter 1 from 'const char [5]' to 'LPCTSTR'错误解决
- LPCTSTR /Data Types/ cannot convert parameter 1 from 'const char [5]' to 'LPCTSTR'
- error C2664: 'CWnd::MessageBoxW' : cannot convert parameter 1 from 'const char [30]' to 'LPCTSTR'
- FAQ: Cannot convert from 'const char [..]' to 'LPCTSTR'
- Cannot convert from 'const char [..]' to 'LPCTSTR'
- error C2664: 'CWnd::MessageBoxW' : cannot convert parameter 1 from 'char *' to 'LPCTSTR'的解决方法
- VC++ 出现错误:cannot convert from const char *' to 'LPCWSTR' 或者'const char [7]' to 'LPCWSTR' 的解决方法
- C++编译遇到参数错误(cannot convert parameter * from 'const char [**]' to 'LPCWSTR')
- vs2008下cannot convert parameter 1 from 'LPCTSTR' to 'const char *'的解决方法
- Vs2010出现错误:cannot convert from 'const char [7]' to 'LPCWSTR'
- error C2664: 'CWnd::MessageBoxW' : cannot convert parameter 1 from 'char *' to 'LPCTSTR'的解决方法
- VC++ 出现错误:cannot convert from 'const char [7]' to 'LPCWSTR'和 宏 _T()用处
- cannot convert parameter 2 from 'const char [12]' to 'LPCWSTR'
- error C2664: 'TextOutW' : cannot convert parameter 4 from 'const char [5]' to 'LPCWSTR'
- VC++ 出现错误:cannot convert from 'const char [7]' to 'LPCWSTR'和 宏 _T()用处
- cannot convert parameter 1 from 'const char [] ' to 'const wchar_t * '的解决方法
- C++编译遇到参数错误(cannot convert parameter * from 'const char [**]' to 'LPCWSTR')