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

翻译 2013年12月02日 18:58:33

这两天在做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

相关文章推荐

VS2005下的 cannot convert parameter 1 from 'const char [5]' to 'LPCTSTR'错误解决

转载自 shenlye最终编辑 忆已矣FAQ: Cannot convert from 'const char [..]' to 'LPCTSTR'QuestionI'm trying to comp...

Vs2010出现错误:cannot convert from 'const char [7]' to 'LPCWSTR'

VC++ 出现错误:cannot convert from 'const char [ ]' to 'LPCWSTR'和 宏 _T()用处  解决方法: 方法一:   菜单栏- 方...

cannot convert parameter * from 'const char [**]' to 'LPCWSTR'

C++编译遇到参数错误(cannot convert parameter * from 'const char [**]' to 'LPCWSTR') 前面的几天一直都在复习着被实习落下...

关于VS2005的error C2664: cannot convert parameter 2 from 'const char [2]' to 'LPCWSTR

一、VS2005设置UNICODE或ASCII字符集问题 VS2005开发程序时,默认的字符集是Unicode。所以如果程序中使用字符常量一定要显示转换为Unicode型,否则编译时报错。 如果你...

使用Unicode下的cannot convert parameter 1 from 'CString' to 'const char *' 解决办法

在网上搜索了很多答案都没有办法得到解决,当然这些办法都可以使得编译通过,例如通过CString.GetBuffer可以得到char*变量指向CString的首地址,但是多种方法在运行时进入xstrin...

C++:invalid conversion from 'unsigned char *' to 'const char *'

可使用reinterpret_cast进行强制转换,示例如下所示: unsigned char *foo(); std::string str; str.append(reinterpret_cas...

cannot convert from android.app.FragmentManager to android.support.v4.app.FragmentManager

vote favorite 1   I am creating an app using Fragments for tablet.I have so far cr...

Tablayout+Viewpager+Fragment实现滑动Tab及cannot convert from Fragment1 to Fragment之解决

实现活动Tab的方式有很多种,今天我们要用的是使用Google 提供的Design support library 库中的Tablayout去实现,Tablayout是 Google I/O 2015...

Unicode字符集与多字节字符集的比较(解决“cannot convert parameter 1 from 'char []' to 'LPCWSTR'”)

今天因为需求又要在Windows下读取目录和文件了,还好以前做过这方面的工作(见“在Linux下和Windows下遍历目录的方法及如何达成一致性操作 ”,封装在Windows和Linux下读取目录和文...

error C2440: 'static_cast' : cannot convert from 'LRESULT (__thiscall CchatDlg::* )(CPoint)' to 'UI

出现这个错误的原因可是“人力不可抗拒”之原因造成的,因为旧版本的 ON_WM_NCHITTEST 宏使用了  UINT (__thiscall CWzButton::* )(CPoint);  类型的...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)