托管类的成员不能为非托管类类型

问题描述:

我正在用c ++ / clr编写程序,我需要对词法分析器进行编码。我在其中:

I'm writing a program in c++/clr and i need to code lexical analyzer. And i have in it these:

std::map <std::string, int> classes = { { "keyword",0 },{ "identifier",0 },{ "digit",0 },{ "integer",0 },{ "real",0 },{ "character",0 },{ "alpha",0 } };
std::vector<std::string> ints = { "0","1","2","3","4","5","6","7","8","9" };
std::vector<std::string> keywords = { "if","else","then","begin","end" };
std::vector<std::string> identifiers = { "(",")","[","]","+","=",",","-",";" };
std::vector<std::string> alpha = { "a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z" };
std::vector<std::string>::iterator iter;

所以这是问题所在:它标记了 ints 关键字等作为错误:
托管成员类不能是非托管类类型

so here is the problem: it marks classes,ints,keywords e.t.c as an error: a member of managed class cannot be of a non-managed class type

我该如何解决?

我看到您正在使用C ++ / clr,所以我认为您有很强的理由。

I see You are using C++ /clr, so I assume You have strong reason for that.

本机类型可以不能成为Managed类的成员。原因是机器必须知道何时销毁/释放由本机代码占用的内存->它们在manage类内部,哪些对象由垃圾收集器销毁。

Native types can not be members of Managed class. Reason for this is that machine have to know when to destroy/deallocate memory occupied by native code -> they are inside manage class which objects are destroyed by garbage collector.

,您可以将托管类型用作类成员...或将本机类型的指针用作类成员-不推荐使用,除非您要进行从Manage到Native的转换,反之亦然。例如:

Anyway, you can use Managed types as class members ... or pointers to native types as class members - not recommended unless You want to make translation from Manage to Native or vice-versa. Here is example:

// compile with: /clr
#include "stdafx.h"
#include <vector>
#include <iostream>
#include <string>

using namespace System;
using namespace System::Collections::Generic;

public ref class MyClass
{
private:
    //std::vector<std::string> ints; // Native C++ types can not be members of Managed class
    List<String^>^ ints; // Managed types can be class members
    std::vector<std::string>* native_ints; // However You can have pointer to native type as class member

public:
    MyClass()
    { // Initialize lists with some values
        ints = gcnew List<String^>(gcnew array<System::String^>{ "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" }); // Managed initialization
        native_ints = new std::vector<std::string>{ "a", "b", "c" }; // Native initialization
    }

    ~MyClass()
    {
        delete native_ints; // Native (not Managed) memory allocation have to be deleted
    }

    void Print()
    {
        Console::WriteLine("Managed List: {0}", String::Join(", ", ints->ToArray()));
        std::cout << "Native vector: " << (*native_ints)[0] << ", " << (*native_ints)[1] << ", " << (*native_ints)[2];
    }
};


int main(array<System::String ^> ^args)
{
    MyClass^ mc = gcnew MyClass();
    mc->Print();

    return 0;
}

控制台输出为:

Managed List: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
Native vector: a, b, c

等效的类型:

std :: map -> SortedDictionary< Tkey,Tvalue>

std :: string -> String

不要忘记 ^ 托管类型