如何在类中初始化数组并为第一个元素设置值



在Arduino中使用。问题就在那里:玩家::坐标[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};

编译器写道:"类播放器"中的"坐标"没有命名类型

您不能在命名空间范围内执行此操作

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

因为数组具有静态存储持续时间并且由编译器初始化为零。

最新更新