0

I have to concatenate two CString variables and two long variables in one CString. I found one Format function that I have used like this:

CString str = "Some Data";

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

But it is giving errors. Here is the error log:

\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

Is there any function like toString() like we use in Java?

johnsyweb
  • 136,902
  • 23
  • 188
  • 247
Ccoder
  • 1
  • 5

2 Answers2

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

Read up on _T and <tchar.h> here: Generic-Text Mappings in Tchar.h .

Roman R.
  • 68,205
  • 6
  • 94
  • 158
0

sprintf ( OutputBuffer, "%s%d", str, 123 ) ;

  1. Use CStringA for ANSI Version.
  2. Use CStringW for Unicode Version.
  3. Use CString for TCHAR Version.

To force CString to be evaluated as CStringA:: Goto, Project->Properties->Configuration Properties->General. On right hand side, you will get "Character Set" Row, change that to "Not set"

Abhineet
  • 5,320
  • 1
  • 25
  • 43