Pregunta

void f(cli::array<PointF> ^points){
    PointF& a = points[0];
    // and so on...
}

Error de compilación en la línea 2.

.\ndPanel.cpp(52) : error C2440: 'initializing' : cannot convert from 'System::Drawing::PointF' to 'System::Drawing::PointF &'
        An object from the gc heap (element of a managed array) cannot be converted to a native reference

¿Cuál es la forma administrada de declarar una variable de referencia?

¿Fue útil?

Solución

Si solo desea declarar una referencia al primer PointF en la matriz, debe usar un referencia de seguimiento (%):

void f(cli::array<PointF>^ points)
{    
    PointF% a = points[0];
}

Otros consejos

Debe usar la plantilla gcroot del archivo vcclr.h :

Estas son muestras de MSDN:

// mcpp_gcroot.cpp
// compile with: /clr
#include <vcclr.h>
using namespace System;

class CppClass {
public:
   gcroot<String^> str;   // can use str as if it were String^
   CppClass() {}
};

int main() {
   CppClass c;
   c.str = gcnew String("hello");
   Console::WriteLine( c.str );   // no cast required
}

// mcpp_gcroot_2.cpp
// compile with: /clr
// compile with: /clr
#include <vcclr.h>
using namespace System;

struct CppClass {
   gcroot<String ^> * str;
   CppClass() : str(new gcroot<String ^>) {}

   ~CppClass() { delete str; }

};

int main() {
   CppClass c;
   *c.str = gcnew String("hello");
   Console::WriteLine( *c.str );
}

// mcpp_gcroot_3.cpp
// compile with: /clr
#include < vcclr.h >
using namespace System;

public value struct V {
   String^ str;
};

class Native {
public:
   gcroot< V^ > v_handle;
};

int main() {
   Native native;
   V v;
   native.v_handle = v;
   native.v_handle->str = "Hello";
   Console::WriteLine("String in V: {0}", native.v_handle->str);
}

Encontrará más aquí

Y aquí se cambia tu código para usar gcroot:

void f(cli::array<gcroot<PointF ^>> points){
     gcroot<PointF ^> a = points[0];
     // and so on... }
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top