简体   繁体   English

Cstring在VS 2012中不起作用,但是如果使用VS 2012打开VC ++ 6.0项目,则Cstring将起作用

[英]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. 我希望有人可以向我解释一些有关Visual Studio 2012中CString的问题。我在VS2012中创建一个新的MFC应用程序,将一个按钮添加到表单中,然后将以下代码添加到该按钮的onclick中。

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 . 在构建解决方案之前,我可以看到它不喜欢Str1.FindStr1.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. 因此,然后我在Visual C ++ 6.0中创建了MFC应用程序,向该窗体添加了一个按钮,并在该按钮的onclick中添加了相同的代码。 I then opened this project using VS2012 and the necessary changes were made to make it compatible. 然后,我使用VS2012打开了该项目,并进行了必要的更改以使其兼容。 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. 现在该代码在Visual Studio 2012中运行良好。我得出的结论(可能是错误的)是,当Visual Studio 2012打开Visual C ++ 6.0项目并进行更改时,它会添加其他文件或#include语句。 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? 谁能阐明为什么该代码在Visual Studio 2012中不起作用,但是如果我使用Visual Studio 2012打开Visual C ++ 6.0项目,该代码将起作用吗?

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. 在您的VC6项目中,您可能未使用Unicode,因此它可以与“ Pear”,“ Apple”等一起使用。更改您的字符串,以便它们使用TEXT()宏。 For example: 例如:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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