Mixed management in C ++

I added a class to my program and tested it. I was very surprised that there were some real mistakes. Here is the code:

#pragma once #include "Iingredient.h" #include <string> #include <vector> using namespace std; ref class Recipe{ private: string partsName; vector<Iingredient> ing; public: Recipe(){} }; 

And here are the errors:

Error 23 C4368 error: cannot define 'partsName' as a member of the managed Recipe: mixed types not supported c: \ users \ user \ documents \ visual studio 2010 \ projects \ smestras2_l1 \ Recipe.h 10 1 file2_L1

Error 24 error C4368: cannot define 'ing' as a member of the managed Recipe: mixed types are not supported c: \ users \ user \ documents \ visual studio 2010 \ projects \ smestras2_l1 \ Recipe.h 11 1 file2_L1

I did a bit of searching and found out about this managed and unmanaged code. How to fix it? Is this related to managed and unmanaged code or not? if so, how?

+4
source share
4 answers

When defining a ref class Recipe you made it manageable. But std::string and std::vector are mind related types. You are trying to declare your own variables in a managed class, but this is prohibited.

You seem to be new to C ++. Just don't use C ++ / CLI. Consider C # if you are targeting .Net or unmanaged C ++.

+4
source

I agree with others: you should not use C ++ / CLI in most cases, you should use C # (or another "normal" managed language) for this (if you want to write a .Net application). C ++ / CLI is useful mainly in special circumstances, for example, in the interaction between managed and unmanaged code.

If you are sure you want to use the C ++ / CLI, you cannot put your own classes in the managed ones. But you can put pointers to native classes:

 ref class Recipe{ private: string* partsName; vector<Iingredient>* ing; }; 

The code above is executed. But you should keep in mind that these are ordinary C ++ native pointers, which means that you need to manually delete them. To make this property, you should read about how destructors and finalizers work in C ++ / CLI.

+11
source

You cannot use unmanaged types in a managed class, the ref keyword, because heap and managed heap are two different types of memory. To solve this specific problem, you can use a managed string type ( System::String^ ). The carrot tells the compiler that the type is a handle to the managed class.

Another way to solve this problem is to use pointers, so the pointer will be on the managed heap, and the actual memory for this object will be in the standard unmanaged heap, where Recipe is located.

To build your Recipe class, you will need to do

 Recipe^ recipe = gcnew Recipe(); 
+3
source

Having no idea about C ++ - cli, I can try to assume that the problem is that you are defining a reference type and trying to use C ++ types inside ( std::string field) and not some equivalent managed type ( String ?).

The reason this can be problematic is because it mixes resource management approaches. Link types are intended for use in the garbage collector, and if you do not implement a destructor or IDisposable , it will simply be ignored as soon as it is proved that the last link is lost. On the other hand, to manage memory in an internal field, you need to call the std::string destructor.

+1
source

Source: https://habr.com/ru/post/1396485/


All Articles