CString为const char *类型转换错误 [英] CString to const char* type casting error

查看:81
本文介绍了CString为const char *类型转换错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我在打字时遇到问题。

这里是代码:

头文件:

Hi All,
I Have a problem in type casting.
here is the code:
header file:

struct userdata
{
    char strUserName[20];
    char strMsg[100];
};



another.cpp文件:


another.cpp file:

CString m_txtSend;
strcpy(uinf.strMsg, m_txtSend);



错误:


The error:

error C2664: 'strcpy' : cannot convert parameter 2 from 'CString' to 'const char *'



另一个错误:

cpp文件:


another Error:
cpp file:

CString sUserName;
sUserName.Format("%s : %s", udata->strUserName, udata->strMsg);



错误:


the error:

error C2664: 'void ATL::CStringT<BaseType,StringTraits>::Format(const wchar_t *,...)' : cannot convert parameter 1 from 'const char [8]' to 'const wchar_t *'
1>        with
1>        [
1>            BaseType=wchar_t,
1>            StringTraits=StrTraitMFC_DLL<wchar_t>
1>        ]
1>        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast



请帮帮我。


please help me.

推荐答案

1 。请参阅 CString 与C样式字符串相关的操作 [ ^ ] 。



2.您正在尝试混合使用ASCII和Unicode类型。使用 CStringA std :: string CStringW std :: wstring ,以避免混淆。
1. See CString Operations Relating to C-Style Strings[^].

2. You are trying to mix ASCII and Unicode types. Use CStringA with std::string, or CStringW with std::wstring, to avoid the confusion.


讨论CStringA和CStringW之间差异的其他答案是正确的。你正在处理ASCII与Uni​​code的问题。



处理这个问题的Microsoft方法涉及使用几个宏。



TCHAR表示一个字符类型,将根据您的项目设置自动解析为ASCII或Unicode。



LPCSTR是一种类型的转换CStringA到const char *

LPCTSTR是一种类型,用于将CString转换为TCHAR *

LPCWSTR是一种类型,用于将CString转换为const wchar_t *



_tcscpy是strcpy的类比,但会解析为TCHAR解析到的任何一个。



为此,微软推荐方式:

The other answers that discuss the difference between CStringA and CStringW are correct. You're dealing with an ASCII vs Unicode issue.

The Microsoft method to deal with this involves the use of a few macros.

TCHAR represents a character type and will automatically resolve to ASCII or Unicode depending on your project settings.

LPCSTR is a type defe to convert CStringA to const char *
LPCTSTR is a type defe to convert CString to TCHAR *
LPCWSTR is a type defe to convert CString to const wchar_t *

_tcscpy is an analog of strcpy, but will resolve to whichever tpye that TCHAR resolves to.

To do it the Microsoft recommended way:
struct userdata
{
    TCHAR strUserName[20];
    TCHAR strMsg[100];
}
 
// another.cpp file:
CString m_txtSend;
_tcscpy(uinf.strMsg, m_txtSend);













CString sUserName;
sUserName.Format("%s : %s", udata->strUserName, udata->strMsg);







如果你必须使用char,那么有办法如果你知道你的字符串将永远是ASCII,那就可以了。








If you must use char, then there is a way to make that work if you know that you're strings will always be ASCII.


struct userdata
{
    char strUserName[20];
    char strMsg[100];
}

// another.cpp file:
CString m_txtSend;
strcpy(uinf.strMsg, (LPCSTR) (CStringA) m_txtSend);





使用格式转换Unicode和ASCII或反之亦然,使用大写%S。





When using format to convert between Unicode and ASCII or vice versa, use the uppercase %S.

CString sUserName;
sUserName.Format("%S : %S", udata->strUserName, udata->strMsg);




您必须使用CString类中的GetBuffer函数,如下所示: />
Hi,
You have to use the function GetBuffer from CString class like this:
int len = m_txtSend.Length();
char* buffer = m_txtSend.getBuffer(len);
strcpy(uiinf.strMsg,buffer); // show to use strcpy_s function that is more secure
m_txtSend.ReleaseBuffer(); // not omit this call 





祝你好运。



Best regards.


这篇关于CString为const char *类型转换错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆