C++ CLI 将数组传递给 class

C++ CLI Passing an array to class

正如标题所说,我必须创建可以接受数组作为参数的 class。

这是 header 文件的当前版本:

public ref class MyClass {

public:
    MyClass() {};  
    MyClass(array<int, 2> ^(&A1), const int &i2) : A1(A1), I2(i2) {};
    String^ Method(); 
    ~MyClass() {};

private: 
    array<int, 2>^ A1 = gcnew array<int, 2>(3, 3) {
        { 1, 1, 1 },
        { 1, 1, 1 },
        { 1, 1, 1 },
    };  
    int I2 = 5;  
};

String^ MyClass::Method() // Simple output for debugging 
{
    String^ OutputText;
    int sum=10;
    OutputText= "OutputText = " + sum;
    return OutputText;
}

截至目前,我收到以下错误:

'$S1': global or static variable may not have managed type 'cli::array ^'

如果我将数组更改为静态,我将得到:

"A1" is not a nonstatic data member or base class of class "MyClass"

Class 必须有两个构造函数。我可以接受矢量解决方案,但我遇到了几乎相同的问题。

这就是我将数组的初始化移动到构造函数中的意思:

public ref class MyClass {

public:
    MyClass() {
        A1 = gcnew array<int, 2>(3, 3) {
                { 1, 1, 1 },
                { 1, 1, 1 },
                { 1, 1, 1 },
            };  
    };  
    MyClass(array<int, 2> ^(&A1), const int &i2) : A1(A1), I2(i2) {};
    String^ Method(); 
    ~MyClass() {};

private: 
    array<int, 2>^ A1;  
    int I2 = 5;  
};