문제

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

라인 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

참조 변수를 선언하는 관리 방법은 무엇입니까?

도움이 되었습니까?

해결책

배열의 첫 번째 Pointf에 대한 참조를 선언하려면 추적 참조 (%):

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

다른 팁

당신은 그것을 사용해야합니다 gcroot 템플릿 vcclr.h 파일:

이들은 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);
}

당신은 더 많은 것을 알게 될 것입니다 여기

그리고 다음은 gcroot를 사용하도록 변경된 코드입니다.

void f(cli::array<gcroot<PointF ^>> points){
     gcroot<PointF ^> a = points[0];
     // and so on... }
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top