简体   繁体   中英

Cstring not working in VS 2012, but will work if VC++ 6.0 project is opened using VS 2012

I am hoping someone can help explain something to me that deals with CString in Visual Studio 2012. I create a new MFC application in VS2012, add a button to the form, and then add the following code to the onclick for that button.

CString Str1;
CString Str2;


Str1 = "Apple";

Str2 = "Pear";

Str1+= Str2;
AfxMessageBox(Str1);

int K = 0;
K = Str1.Find("Pear");

Str1.Format("%d", K);
Str1.TrimLeft();
AfxMessageBox(Str1);

Before I build the solution I can see that it does not like Str1.Find and Str1.Format . It gives this error...

error C2664: 'int ATL::CStringT<BaseType,StringTraits>::Find(wchar_t,int) throw() const' :   
cannot convert parameter 1 from 'const char [5]' to 'wchar_t'
      with
      [
          BaseType=wchar_t,
         StringTraits=StrTraitMFC_DLL<wchar_t>
      ]
      There is no context in which this conversion is possible

So then I created an MFC application in Visual C++ 6.0, added a button to the form, and added the same code in the onclick for the button. I then opened this project using VS2012 and the necessary changes were made to make it compatible. Now that code works perfectly fine in Visual Studio 2012. I have concluded (maybe incorrectly) that when Visual Studio 2012 is opening the Visual C++ 6.0 project and making the changes it is adding something like another file or an #include statement. I've compared the two projects and I cannot for the life of me figure out what is going on. Could anyone shed some light on why this code does not work in Visual Studio 2012, but will work if I open the Visual C++ 6.0 project with Visual Studio 2012?

In your VC6 project you are probably not using Unicode and so it works with "Pear", "Apple", etc. Change your strings so that they use the TEXT() macro. For example:

Str1.Find(TEXT("Pear"));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM