c++ 使用 CLI 数组初始化值静态 int

c++ initialize values static int with CLI array

本文关键字:静态 int 初始化 数组 使用 CLI c++      更新时间:2023-10-16

所以我有 2 个数组,都是二维 cli::arrays。初始化 cli::array 的正确语法是什么。我在下面的示例中尝试过,但这不起作用。

//Cords.h
ref class Cords {
private:
     static array<int,2>^ Xcord = gcnew array<int,2>(4,4);  // [4][4]
     static array<int,2>^ Ycord = gcnew array<int,2>(4,4);  // [4][4]
public:
     Cords();
     static int getX(void);
     static int getY(void);
};
int Cords::Xcord[0][0] = 4234; //On these lines is the mistake
int Cords::Ycord[0][0] = 2342; //On these lines is the mistake

所以我用静态构造函数解决了这个问题,我注意到你应该输入 [0,0] 而不是 [0][0]。我习惯了普通的 C 数组。

//Cords.h
ref class Cords {
private:
static array<int,2>^ Xcord = gcnew array<int,2>(4,4);  // [4][4]
static array<int,2>^ Ycord = gcnew array<int,2>(4,4);  // [4][4]
static Cords() {         //static constructor to initialize values
      Xcord[0,0] = 4234; // [0,0] instead of [0][0]
      Ycord[0,0] = 2342;
      ...
    }
public:
     Cords();
     static int getX(void);
     static int getY(void);
};