[英]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.Find
和Str1.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.