如何在托管 C++ 参考中使用名称空间系统 class

How to use namespace System in a managed c++ reference class

我正在尝试按照本教程创建托管 C++ class msdn.microsoft.com 但是当我尝试使用名称空间系统时 System::String *_msg它总是给我一个错误

An ordinary pointer to a C++/Cli ref class or interface class is not allowed

Hello.h

using namespace System;
ref class Hello
{

public:
    System::String *_msg;
    Hello(System::String *Msg);
};

hello.cpp 文件

#include "Hello.h"
using namespace System;


Hello::Hello(System::String *Msg)
{
    Msg = _msg;
    Console::WriteLine(Msg);
}
void main() {
    Hello ^ h = gcnew Hello("hello world");

}

使用托管指针 ^ 符号代替非托管 * 指针:

using namespace System;

    ref class Hello
    {
    public:
        System::String ^_msg;
        Hello(System::String ^Msg);
    };
    #include "Hello.h"
    using namespace System;

    Hello::Hello(System::String ^Msg)
    {
        Msg = _msg;
        Console::WriteLine(Msg);
    }
    void main() {
        Hello ^ h = gcnew Hello("hello world");
    }