我要发帖 回复

新手上路

1

主题

12

积分

0

专家分

:

私信
发表时间 : 2009-11-7 10:56:59 | 浏览 : 1443    评论 : 1
我是初学者,在OPENGL中出现 'auxInitWindowW' : cannot convert parameter 1 from 'const char [13]' to 'LPCWSTR'怎样能解决?谢谢各位高手

最近VR访客

tan_dunming 评论于2009-11-10 13:25:33
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".
   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.
追求卓越

手机版|VR开发网 ( 津ICP备18009691号 ) 统计 网安备12019202000257

GMT+8, 2020-10-1 07:49 PM

返回顶部