error:cannot convert parameter 1 from 'const char[12]' 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 topass a multi-byte string (const char [12]) to a function which expects aunicode string (LPCTSTR). The LPCTSTR type extends to const TCHAR*, where TCHARis char when you compile for multi-byte and wchar_t for unicode. Since thecompiler doesn't accept the char array, we can safely assume that the actualtype of TCHAR, in this compilation, is wchar_t.
Resolution
     You will have to do one of two things:
               <1> Change yourproject configuration to use multibyte strings. Press ALT+F7 to open theproperties, and navigate to          ConfigurationProperties > General. Switch Character Set to "Use Multi-Byte CharacterSet".
              <2> Indicate that thestring literal, in this case "Hello world!" is of a specificencoding. This can be done through either prefixing it with L, such asL"Hello world!", or surrounding it with the generic _T("Helloworld!") macro. The latter will expand to the L prefix if you arecompiling 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 buildconfiguration. This problem can be resolved primarily by using solution #2, butin some cases also #1. A lot of the Microsoft provided libraries, such as thePlatform SDK, have got two variations of each function which takes strings asparameters. 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 wouldbe MessageBoxA (ASCII). Which of these functions is actually used when youcompile your application, depends on the setting described in resolution #1above.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值