繁体   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

我希望有人可以向我解释一些有关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);

在构建解决方案之前,我可以看到它不喜欢Str1.FindStr1.Format 它给出了这个错误...

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

因此,然后我在Visual C ++ 6.0中创建了MFC应用程序,向该窗体添加了一个按钮,并在该按钮的onclick中添加了相同的代码。 然后,我使用VS2012打开了该项目,并进行了必要的更改以使其兼容。 现在该代码在Visual Studio 2012中运行良好。我得出的结论(可能是错误的)是,当Visual Studio 2012打开Visual C ++ 6.0项目并进行更改时,它会添加其他文件或#include语句。 我已经比较了两个项目,但我无法一生都知道发生了什么。 谁能阐明为什么该代码在Visual Studio 2012中不起作用,但是如果我使用Visual Studio 2012打开Visual C ++ 6.0项目,该代码将起作用吗?

在您的VC6项目中,您可能未使用Unicode,因此它可以与“ Pear”,“ Apple”等一起使用。更改您的字符串,以便它们使用TEXT()宏。 例如:

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

暂无
暂无

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

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