C++ cli 在C+中使用结构+/CLI

C++ cli 在C+中使用结构+/CLI,c++-cli,C++ Cli,我有以下代码 ref class A { typedef ref struct All { std::string x; }All_t; }; 在我的程序中,我以以下方式使用它 A::All_t t; t.X = "H"; 此声明将错误抛出为 error C4368: cannot define 'x' as a member of managed 'A::All': mixed types are not supported 我知道我在托管代码中声明了本机变量,这是不允许的,但现在

我有以下代码

ref class A
{

typedef ref struct All
{
 std::string x;
}All_t;

};
在我的程序中,我以以下方式使用它

A::All_t t;
t.X = "H";
此声明将错误抛出为

error C4368: cannot define 'x' as a member of managed 'A::All': mixed types are not supported
我知道我在托管代码中声明了本机变量,这是不允许的,但现在我想知道为了使我的结构适合托管项目而需要做的更改


谢谢。

我假设您最初有
std::string x
not
std::string*x
(因为使用字符串指针不会生成该错误)。不允许在托管类型中直接嵌入本机类型,但允许(通过指针)间接嵌入本机类型。请参见:

在我修复了您的示例中的编译器错误后,它生成时没有错误:

#include "stdafx.h"
#include <string>
using namespace System;

ref class A
{
public:
    typedef ref struct All
    {
        std::string * x;
    }All_t;

};

int main(array<System::String ^> ^args)
{
    A::All_t t;
    t.x = new std::string("H");

    return 0;
}
#包括“stdafx.h”
#包括
使用名称空间系统;
参考等级A
{
公众:
typedef ref struct All
{
std::string*x;
}全部;;
};
int main(数组^args)
{
答:全部;
t、 x=新标准::字符串(“H”);
返回0;
}