String的C++含义^

2 .net printing string c++-cli visual-c++

我正在尝试为标签(Windows窗体)制作一个getter.

myForm.h中:

public: String^ getLabel1() { return label1->Text; };
Run Code Online (Sandbox Code Playgroud)

但是当我尝试打印它时,它将无法编译!

myForm.cpp中:

void Main(array<String^>^args)
{
    Application::EnableVisualStyles();
    Application::SetCompatibleTextRenderingDefault(false);

    Test::MyForm form;
    Application::Run(%form);

    form.setLabel1();
    std::cout << form.getLabel1() << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

我想我返回错误的类型(String ^),但我找不到'^'字符上的任何内容,当我尝试返回a时std::string,我无法编译它!有人能帮我吗?

Dai*_*Dai 9

String^是"引用GC对象" - 存在于托管堆中的对象(即".NET对象").与String*(指向System::StringCLR对象的指针或std::string(C++标准库字符串本地)相比.

C++ STL cout流不支持输出System::String,您必须先将其封送到兼容类型,或者使用Console::WriteLine.

要重新迭代sp2danny在评论中所说的内容,这不是 C++,这是C++/CLI,它可以被认为是C++ + C语言替代C#的C语言,有利于能够使用现有的C++代码源形式,包括STL.