如何在托管c ++引用类中使用命名空间System

时间:2016-11-23 15:22:09

标签: c++-cli

我正在尝试按照本教程创建托管C ++类msdn.microsoft.com但是当我尝试使用命名空间System System :: String * _msg 时,它总是给我一个错误,指出< / p>

  

指向C ++ / Cli ref类或接口类的普通指针不是   允许

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");

}

1 个答案:

答案 0 :(得分:3)

而不是非托管*指针使用托管指针^符号:

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");
    }