在VC ++中连接CString和Long?

时间:2013-02-19 05:19:33

标签: c++ casting atl tostring cstring

我必须将两个CString变量和两个long变量连接在一起 CString。我找到了一个我用过的格式函数:

CString str = "Some Data";

str.Format("%s%d", str, 123);

但它给出了错误。这是错误日志:

\AudWinSockXCtrl.cpp(410) : error C2440: 'initializing' : cannot convert from 'const char [10]' to 'ATL::CStringT'

        with

        [

            BaseType=wchar_t,

            StringTraits=StrTraitMFC

        ]

        Constructor for class 'ATL::CStringT' is declared 'explicit'

        with

        [

            BaseType=wchar_t,

            StringTraits=StrTraitMFC

        ]

.\AudWinSockXCtrl.cpp(411) : error C2664: 'void ATL::CStringT::Format(const wchar_t *,...)' :
cannot convert parameter 1 from 'const char [5]' to 'const wchar_t *'
        with


        [

            BaseType=wchar_t,

            StringTraits=StrTraitMFC

        ]

        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or
function-style cast

.\AudWinSockXCtrl.cpp(414) : error C2664: 'void ATL::CStringT::Format(const wchar_t *,...)' :
cannot convert parameter 1 from 'const char [4]' to 'const wchar_t *'

        with

        [

            BaseType=wchar_t,

            StringTraits=StrTraitMFC

        ]

        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or
function-style cast

我们在Java中使用像toString()这样的函数吗?

2 个答案:

答案 0 :(得分:1)

CString str = _T("Some Data");
str.Format(_T("%s%d"), str, 123);

在此处阅读_T<tchar.h>Generic-Text Mappings in Tchar.h

答案 1 :(得分:0)

sprintf(OutputBuffer,“%s%d”,str,123);

  1. 将CStringA用于ANSI版本。
  2. 将CStringW用于Unicode版本。
  3. 将CString用于TCHAR版本。
  4. 强制CString被评估为CStringA ::  Goto,Project-&gt; Properties-&gt; Configuration Properties-&gt; General。  在右侧,您将获得“字符集”行,将其更改为“未设置”