7

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?

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
Anatoly
  • 97
  • 1
  • 1
  • 3
  • Path of least resistance is probably to use the .net analogues of the C++ data structures. – user4581301 May 10 '18 at 19:27
  • that's what im asking. how can i replace map/vector/iterator....? – Anatoly May 10 '18 at 19:29
  • https://stackoverflow.com/questions/18895727/c-vector-like-class-in-c-sharp –  May 10 '18 at 19:32
  • 1
    Move these declarations into a plain C++ class. Your ref class needs a *pointer* to an instance of that class. Allocate in the constructor, release in the destructor and finalizer. – Hans Passant May 10 '18 at 20:58
  • Possible duplicate of [Managed class with a non-managed member](https://stackoverflow.com/questions/20173189/managed-class-with-a-non-managed-member) – Sheng Jiang 蒋晟 May 10 '18 at 20:58
  • C++/cli can use regex and you could fairly simply convert the C# code here https://stackoverflow.com/questions/673113/poor-mans-lexer-for-c-sharp – codebender May 10 '18 at 21:33

1 Answers1

11

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:

And do not forget about ^ for managed types

atrelinski
  • 432
  • 2
  • 9