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];
}

其他提示

您需要使用 vcclr.h 文件中的 gcroot 模板:

这些是来自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