Visual c++ visualc&x2B+;CLR:使用非基本数据类型作为属性 < P>在Visual Studio 2013上使用Visual C++。 项目类型:“CLR空项目”

Visual c++ visualc&x2B+;CLR:使用非基本数据类型作为属性 < P>在Visual Studio 2013上使用Visual C++。 项目类型:“CLR空项目”,visual-c++,c++-cli,clr,Visual C++,C++ Cli,Clr,我试图创建一个包含数据类型std::string和std::array的私有属性的类 编译器给了我以下错误: “托管类的成员不能是非托管类类型” 我进行的研究使我相信CLR项目只允许使用原始数据类型作为类属性。 不幸的是,我一直无法找到解决方法 该错误在.h文件中给出。我已复制了下面的.h文件: { public: PlayableSet(); ~PlayableSet(); bool verifySolutionSet(std::string solution); std::array&

我试图创建一个包含数据类型
std::string
std::array
的私有属性的类

编译器给了我以下错误:

“托管类的成员不能是非托管类类型”

我进行的研究使我相信CLR项目只允许使用原始数据类型作为类属性。
不幸的是,我一直无法找到解决方法

该错误在.h文件中给出。我已复制了下面的.h文件:

{
public:

PlayableSet();
~PlayableSet();

bool verifySolutionSet(std::string solution);

std::array<int, 5> getPlayableIntegers();
std::array<char, 4> getPlayableOperators();
std::string getPlayableString();
std::array<int, 9> getPlayableSetIntegerCount();
std::array<int, 4> getPlayableSetOperatorCount();

private:

const static std::array<char, 4> OPERATOR_ARRAY ;
std::array<int, 5> PlayableIntegers;
std::array<char, 4> PlayableOperators = { '+', '-' };
std::string PlayableString = "";
std::array<int, 9> PlayableSetIntegerCount;
std::array<int, 4> PlayableSetOperatorCount = { 1, 1 }; 
}
{
公众:
可玩集();
~PlayableSet();
布尔验证解决方案集(标准::字符串解决方案);
std::数组getPlayableIntegers();
std::数组getPlayableOperators();
std::string getPlayableString();
std::数组getPlayableSetIntegerCount();
std::数组getPlayableSetOperatorCount();
私人:
常量静态std::数组运算符\u数组;
std::数组可播放整数;
数组可播放运算符={'+','-'};
std::string PlayableString=“”;
std::数组可播放setintegercount;
std::array PlayableSetOperatorCount={1,1};
}
注:

  • 我已经包括了数组和字符串
  • 显示所有私有属性的错误
  • PlayableOperators和PlayableSetOperatorCount仅使用前两个元素初始化。这是有意的

    • 我的建议是,如果要编写托管类,请编写托管类。如果要编写非托管类,请编写非托管类

      C++/CLI允许混合托管代码和非托管代码,但有一些限制。在编写单个类的定义时,您通常希望坚持使用其中一个类

      • 如果要编写托管类,请编写托管类。
        • 如果要将该类编写为托管类(
          public ref class foo
          ),那么它应该为其数据成员使用托管类型。如果需要使用非托管类型访问该类,请在accessor/mutator方法中执行与托管类型的转换
        • 在本例中,这将是
          System::String^
          cli::array^foo=gcnew cli::array(length)
          List^foo=gcnew List()
      • 如果要编写非托管类,请编写非托管类。
        • 如果要将该类编写为非托管(
          classfoo
          ),则它应该为其数据成员使用非托管类型

      如果您发现自己一直在使用托管/非托管转换,那么可能需要将类作为另一个来编写。或者考虑更改类定义,将类的职责分成托管类和非托管类。

      错误消息非常明确,C++ + CLI编译器禁止此。这太危险了,垃圾收集器在压缩堆时会移动内存中的对象。这会使指向非托管对象的任何指针无效,而无法更正此类指针。必须使用string*或string&因此本机对象也不能移动。在这种情况下,您希望将它们封装在一个本地的C++结构或类中,这样您只有一个指针。在构造函数中初始化它,将析构函数和终结器添加到release中。