关闭

FAQ: Cannot convert from 'const char [..]' to 'LPCTSTR'

标签: 网络编程visual studiocompiler
733人阅读 评论(0) 收藏 举报
分类:

这两天在做visual c++网络编程,从vc6.0迁移到visual studio2010过程中遇到了一些问题。

这是我要编译的一个代码段,

MessageBox("服务器地址或者端口不能为空");

当我编译工程之后出现了这样的错误:

error C2664: 'CWnd::MessageBoxW' : cannot convert parameter 1 from 'const char []' to 'LPCTSTR'

然后查了一下:

这个问题的原因是当试图将一个多字节字符串传送给一个需要unicode类型字符串(LPCTSTR)为参数的函数时,LPCTSTR类型就扩展为TCHAR*,当编译多字节字符串和unicode类型的wchar_t时TCHAR是char型。因为编译器不接受char数组,所以我们假设TCHAR类型在本次编译中为wchar_t型

(This error message means that you are trying to pass a multi-byte string (const char[]) 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.)

解决的办法是:

1.改变工程设置使它能够用多字节字符串:按ALT+F7打开属性工程窗口,然后点击Configuration Properties>General.将Switch Character Set改为“Use Multi-Byte Character Set”。(其实我改了之后已经可以解决问题啦!)

2.

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.
变化:

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.

这是原文章的链接:http://social.msdn.microsoft.com/Forums/vstudio/en-US/c1b08c0a-a803-41c3-ac8c-84eba3be1ddb/faq-cannot-convert-from-const-char-to-lpctstr?forum=vclanguage
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:9634次
    • 积分:225
    • 等级:
    • 排名:千里之外
    • 原创:10篇
    • 转载:1篇
    • 译文:2篇
    • 评论:1条