Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

a member of managed class cannot be of a non-managed class type

Tags:

c++-cli

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;

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

how can i fix this?

like image 611
Anatoly Avatar asked May 10 '18 19:05

Anatoly


1 Answers

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

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.

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

And console output is:

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

Types equivalents:

  • std::vector -> List<T>

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

  • std::string -> String

And do not forget about ^ for managed types

like image 107
atrelinski Avatar answered Dec 31 '22 20:12

atrelinski