C++ 从 LPCTSTR 转换为 const char *

2021-12-25 00:00:00 types type-conversion c++

我在 MSVC2008 MFC 中有这个问题.我正在使用 unicode.我有一个函数原型:

MyFunction(const char *)

我称之为:

MyfunFunction(LPCTSTR wChar).

<块引用>

错误:无法将参数 1 从LPCTSTR"转换为const char *"

如何解决?

解决方案

由于您使用的是 MFC,您可以轻松地让 CString 进行从 charTCHAR 的自动转换>:

MyFunction(CString(wChar));

无论您的原始字符串是基于 char 还是基于 wchar_t,这都有效.

看来我最初的回答与您所要求的相反.轻松修复:

MyFunction(CStringA(wChar));

CStringACString 的一个版本,它专门包含 char 字符,而不是 TCHAR.还有一个 CStringW 保存 wchar_t.

I have this problem in MSVC2008 MFC. I′m using unicode. I have a function prototype:

MyFunction(const char *)

and I'm calling it:

MyfunFunction(LPCTSTR wChar). 

error:Cannot Convert Parameter 1 From 'LPCTSTR' to 'const char *'

How to resolve it?

解决方案

Since you're using MFC, you can easily let CString do an automatic conversion from char to TCHAR:

MyFunction(CString(wChar));

This works whether your original string is char or wchar_t based.

Edit: It seems my original answer was opposite of what you asked for. Easily fixed:

MyFunction(CStringA(wChar));

CStringA is a version of CString that specifically contains char characters, not TCHAR. There's also a CStringW which holds wchar_t.

相关文章