如何声明handel数组?

TTG*_*oup 0 .net c++ arrays collections c++-cli

我想声明一个handel数组,如下面的代码:

using namespace System::Drawing;
ref class B 
{
    Bitmap^ b[];

    B()
    {
        b = new Bitmap^[10];
    }
};
Run Code Online (Sandbox Code Playgroud)

但是在编译时它会抛出错误

error C2728: 'System::Drawing::Bitmap ^' : a native array cannot contain this managed type
error C4368: cannot define 'b' as a member of managed 'B': mixed types are not supported
error C2728: 'System::Drawing::Bitmap ^' : a native array cannot contain this managed type
error C2440: '=' : cannot convert from 'System::Drawing::Bitmap ^*' to 'System::Drawing::Bitmap ^[]'
Run Code Online (Sandbox Code Playgroud)

有人可以告诉我正确的方式来声明一个handel数组吗?

非常感谢!

T&T组

pst*_*jds 5

你需要使用gcnew 这个.Net数组,而不是C++数组,因为这是一个托管类型的数组,而不是一个本机类型的数组.我没有编译器方便测试此代码,但我相信这将是这样做的方法.

using namespace System::Drawing;
ref class B 
{
private:
    array<Bitmap^>^ b;

public:
    B()
    {
        b = gcnew array<Bitmap^>(10);
    }
};
Run Code Online (Sandbox Code Playgroud)