无法访问游戏对象 "reference to non-static member function must be called"

Can't access game objects "reference to non-static member function must be called"

我正在制作贪吃蛇游戏,我正在尝试将我的主要函数重构为对 Game 对象的调用(Game.run() 或类似的东西)。我将 post 下面的所有代码,但基本设置是我有一个 Snake 对象、一个 Window(它为我渲染)、一个 Food 对象和几个计时器。在当前的主要功能中,我在开始时实例化所有这些,然后稍后使用它们。我想将所有这些重构到一个游戏 class 中,其中有 Snake、Window、Food 和 Timers 作为实例变量。我遇到的问题是我以后无法访问这些成员!我得到的错误是 "C++ "必须调用对非静态成员函数的引用。

这里是原代码的一部分:

int main( int argc, char* args[] ) {
  Window window("Snake", GRID_WIDTH, GRID_HEIGHT, GRID_STRIDE);
  Snake snake(window, GRID_STRIDE, 20, 20, 255, 255, 255, 255);
  Food food(window, GRID_WIDTH, GRID_HEIGHT, GRID_STRIDE, 192, 192, 192, 255, time(0));
  Timer fpsTimer;
  Timer capTimer;

  int countedFrames = 0;
  fpsTimer.start();
  bool updateDisplay = true;
  while (!window.isClosed()) {
    capTimer.start();
    pollEvents(window, snake);
    if (updateDisplay && !snake.isDead()) {
      window.draw();
      snake.draw();
      food.draw();
      checkCollisions(snake, food);
      snake.checkSelfEat();
    }
}

这很好用。但是当我将其重构为游戏时 class:

//game.h

class Game {
  public:
    Game(int g_width, int g_height, int g_stride, int fps);
    virtual ~Game() {};
    void run();

  private:
    int grid_width, grid_height, grid_stride, screen_fps, screen_ticks_per_frame;
    Window window(std::string title, int g_width, int g_height, int g_stride);
    Snake snake(Window &window, int g_stride, int x, int y, int r, int g, int b, int a);
    Food food(Window &window, int g_width, int g_height, int g_stride, int r, int g, int b, int a, int random_seed);
    Timer fpsTimer, capTimer;
};

实施:

// game.cpp

Game::Game(int g_width, int g_height, int g_stride, int fps) :
grid_width(g_width), grid_height(g_height), grid_stride(g_stride), screen_fps(fps) {
  Window window("Snake", grid_width, grid_height, grid_stride);
  Snake snake(window, grid_stride, 20, 20, 255, 255, 255, 255);
  Food food(window, grid_width, grid_height, grid_stride, 192, 192, 192, 255, time(0));
  Timer fpsTimer;
  Timer capTimer;
  screen_ticks_per_frame = 1000 / screen_fps;
}

void Game::run() {
  int countedFrames = 0;
    fpsTimer.start();
    bool updateDisplay = true;
    while (!window.isClosed()) {
      capTimer.start();
      pollEvents(window, snake);
      if (updateDisplay && !snake.isDead()) {
        window.draw();
        snake.draw();
        food.draw();
        checkCollisions(snake, food);
        snake.checkSelfEat();
      }
};

我真的很想了解这一点,当我尝试使用 GoogleTest 测试装置时也遇到了同样的错误。如果有人能告诉我我做错了什么,那就太好了!

例如,一个错误是:

错误:必须调用对非静态成员函数的引用 while (!window.isClosed()) {

带下划线的window。 window、snake、food、timer 的每个实例都出现错误,实例带有下划线。

在此方面提供一些帮助将不胜感激,如果我在这方面不够清楚,请见谅post。

您的代码中存在两个基本问题。首先是您要声明 private 函数,而不是成员变量。声明成员变量只需要使用类型和变量名,例如:

class Game {
  // public omitted for brevity
  private:
    int grid_width, grid_height, grid_stride, screen_fps, screen_ticks_per_frame;
    Window window;
    Snake snake;
    Food food;
    Timer fpsTimer, capTimer;
};

第二个问题是,在构造函数主体中,您声明了与 "members" 同名的 local 变量 - 尽管这些变量与本地变量不同在构造函数完成后被销毁。
您应该使用 member initializer list,就像您已经为 int 成员所做的那样,例如:

Game::Game(int g_width, int g_height, int g_stride, int fps) :
grid_width(g_width), grid_height(g_height), grid_stride(g_stride), 
screen_fps(fps), window("Snake", grid_width, grid_height, grid_stride), 
snake(window, grid_stride, 20, 20, 255, 255, 255, 255), 
food(window, grid_width, grid_height, grid_stride, 192, 192, 192, 255, time(0)),
screen_ticks_per_frame(1000 / screen_fps){
  // no need to repeat the Timers
}