user987316
user987316

Reputation: 942

Working with structures in C++/CLI

I have following code

ref class A
{

typedef ref struct All
{
 std::string x;
}All_t;

};

in my program I am using it in following manner

A::All_t t;
t.X = "H";

This declaration throwing error as

error C4368: cannot define 'x' as a member of managed 'A::All': mixed types are not supported

I understand that I am declaring native varible inside managed code which is not allowed but now I would like to know changes I will need to make to make my structure suitable to managed project.

Thanks.

Upvotes: 0

Views: 302

Answers (1)

Matt Smith
Matt Smith

Reputation: 17444

I'm assuming you originally had std::string x; not std::string *x (since using the pointer to string does not generate that error). You are not allowed to directly embed a native type in a managed type, but you are allowed to indirectly have one (via a pointer) See:

http://msdn.microsoft.com/en-us/library/xhfb39es(v=vs.80).aspx

After I fixed the compiler errors in your sample, it builds without error:

#include "stdafx.h"
#include <string>
using namespace System;

ref class A
{
public:
    typedef ref struct All
    {
        std::string * x;
    }All_t;

};

int main(array<System::String ^> ^args)
{
    A::All_t t;
    t.x = new std::string("H");

    return 0;
}

Upvotes: 2

Related Questions