1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Converting error from 'CString' to 'const char *

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

  1. answerme

    answerme New Member

    Joined:
    Dec 17, 2007
    Messages:
    114
    Likes Received:
    0
    Trophy Points:
    0
    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δЭ ~

    Joined:
    May 5, 2009
    Messages:
    1,343
    Likes Received:
    55
    Trophy Points:
    0
    Occupation:
    STUDENT !
    Location:
    Orissa, INDIA
    Home Page:
    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

    Joined:
    Nov 10, 2009
    Messages:
    93
    Likes Received:
    5
    Trophy Points:
    0
    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