c语言2446错误,vs2005下的Error C2446

在使用VS2005编译代码时遇到Error C2446错误,意味着尝试将一个多字节字符串传递给期望Unicode字符串的函数。解决方法包括修改项目配置以使用多字节字符串或明确指定字符串的编码。可以通过设置项目属性为使用多字节字符集,或者通过在字符串前添加L前缀或使用generic_T宏来指示特定编码。

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值