如何从 class 初始化数组并将值设置为第一个元素?

How to initialize array out of the class and set a value to the first element?

在Arduino中使用。问题在那里:Player::coords[0] = {0, 0};

标题文件:

#ifndef game_h
#define game_h
#include <Arduino.h>

class Player {
  public:
    int getScore();
    static int coords[3250][2];
    coordinates: x, y

  private:
    static int score;
};

#endif

Cpp 文件:

#include "game.h"

int Player::score = 1;

int Player::getScore() {
  return this->score;
}

int Player::coords[3250][2];
Player::coords[0] = {0, 0};

编译器写道:'class Player' 中的 'coords' 没有命名类型

您不能在命名空间范围内这样做

int Player::coords[3250][2];
Player::coords[0] = {0, 0};

实际上这些语句等同于这个

int Player::coords[3250][2] = { { 0, 0 } };

int Player::coords[3250][2] = {};

甚至只是

int Player::coords[3250][2];

因为数组具有静态存储持续时间,并且 zero-initialized 由编译器提供。