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

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

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




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



(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”。(其实我改了之后已经可以解决问题啦!)


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.


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...

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


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型,否则编译时报错。 如果你...

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...

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

出现这个错误的原因可是“人力不可抗拒”之原因造成的,因为旧版本的 ON_WM_NCHITTEST 宏使用了  UINT (__thiscall CWzButton::* )(CPoint);  类型的...
  • zmq5411
  • zmq5411
  • 2011年03月29日 15:16
  • 2352

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

出现这个错误的原因可是“人力不可抗拒”之原因造成的,因为旧版本的 ON_WM_NCHITTEST 宏使用了   UINT (__thiscall CWzButton::* )(CPoint); ...
您举报文章:FAQ: Cannot convert from 'const char [..]' to 'LPCTSTR'