1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

Converting error from 'CString' to 'const char *

Discussion in 'MFC' started by answerme, Dec 28, 2009.

  1. answerme

    answerme New Member

    Hi I am getting a conversion error

    Code:
    CString Instring;
    CString Automesg[2];
    Automesg[0]="test 1";
    Automesg[1]="Test Example2";
    if(strcmp(Instring,Automesg[0])==0) //HERE IS THE ERROR FOR InString Variable
    	{
    		Sleep(1000);
             }
    
    [B]error C2664: 'strcmp' : cannot convert parameter 1 from 'CString' to 'const char *'	[/B]
    Can anyone resolve it
     
  2. SaswatPadhi

    SaswatPadhi ~ Б0ЯИ Τ0 С0δЭ ~

    You can use the "compare" function.

    Say, you have 2 CStrings StringA and StringB.
    You can compare them like :
    Code:
    if ( StringA.compare(StringB) == 0 ) cout << StringA << " and " << StringB << " are not equal.";
    
    So, your code can be modified as :
    Code:
    CString Instring;
    CString Automesg[2];
    Automesg[0]="test 1";
    Automesg[1]="Test Example2";
    if(Instring.compare(Automesg[0])==0)
    {		Sleep(1000);	}
     
  3. Gene Poole

    Gene Poole New Member

    I think the problem is likely a UNICODE issue. With UNICODE enabled in your project, CStrings default to "wide" character strings. A "wide" character is 2 bytes whereas normal char is 1 byte. You can use the "wide" versions of the comparison functions, wcscmp() v. strcmp(), or to keep everything portable, use the TCHAR macros. _tcscmp() is a macro that will evaluate to strcmp() in MCBS builds or wcscmp() in UNICODE builds.
     

Share This Page