的问题是,你在游戏中至少有两个不同的字符串类在这里
的WinForms(你正在使用显然是为您的GUI )使用.NET System::String
class无处不在。因此,Label.Text
属性正在获取/设置一个.NET System::String
对象。
你说在GetSomething()
方法返回一个std::string
对象的问题。 std::string
类基本上是C++的内置字符串类型,作为标准库的一部分提供。
这两个类都很好,很好地服务于各自的目的,但它们不直接兼容。这是什么(第二次尝试的)编译器的消息要告诉你:
error C2664: void System::Windows::Forms::Control::Text::set(System::String ^)
: cannot convert parameter 1 from std::basic_string<_Elem,_Traits,_Ax>
to System::String ^
用简单的英语改写:
error C2664: cannot convert the native std::string
object passed as parameter 1 to a managed System::String
object, required for the Control::Text
property
事实是,你真的不应该将两者混合字符串类型。由于WinForms基本上强制你的字符串类型,至少对于与GUI交互的任何代码来说,这是我要标准化的一个。所以如果可能的话,重写GetSomething()
方法返回一个System::String
对象;例如:
using namespace System;
...
String^ GetSomething()
{
int id = 0;
array <String^>^ Blah = gcnew array<String^>{"test", "fasf", "hhcb"};
return Blah[id];
}
...
// use the return value of GetSomething() directly because the types match
this->Label1->Text = "blah blah: " + GetSomething();
如果这是不可能的(例如,如果这是库代码有很少或没有与你的GUI),那么你需要explicitly convert one string type to the other:
#include <string> // required to use std::string
...
std::string GetSomething()
{
int id = 0;
std::string Blah[] = {"test", "fasf", "hhcb"};
return Blah[id];
}
...
// first convert the return value of GetSomething() to a matching type...
String^ something = gcnew String(GetSomething().c_str());
// ...then use it
this->label1->Text = "blah blah: " + something;
可以显示GetSomething代码? – billz
您是否#include? –
@RanEldan我只是这样做,另一个错误出现:\t错误C2664:'System :: Windows :: Forms :: ToolStripItem :: Text :: set':无法将参数1从'std :: basic_string <_Elem,_Traits ,_Alloc>'到'System :: String ^' – Kyle