2
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

参照変数を宣言する管理された方法は何ですか?

4

3 に答える 3

3

配列内の最初の PointF への参照を宣言するだけの場合は、追跡参照(%)を使用する必要があります。

void f(cli::array<PointF>^ points)
{    
    PointF% a = points[0];
}
于 2008-10-29T13:18:24.987 に答える
1

ファイル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);
}

詳細はこちら

于 2008-10-29T06:40:21.950 に答える
0

gcroot を使用するように変更されたコードは次のとおりです。

void f(cli::array<gcroot<PointF ^>> points){
     gcroot<PointF ^> a = points[0];
     // and so on... }
于 2008-10-29T07:53:20.383 に答える