1

私はゲーム開発の初心者で、C++ で簡単なプラットフォーム ゲームを作成したいと考えています。問題は、2 つのクラス (ゲームとグラフィックス) を作成するときに、既に Graphics.h を Game.h に含めているため、Game.h を Graphics.h に含めることができないことです。誰でも私を助けることができますか?コード: Game.h:

#pragma once

#include "Graphics.h"

struct Game {
    Game();

    void init();
    void handle();

    bool running;

    Graphics g;
};

Graphics.h:

#pragma once

#include <SDL.h>

struct Graphics {
    SDL_Surface* screen;

    void init();

    void rect(SDL_Rect rect, Uint32 color);
    void rect(SDL_Rect rect, int r, int g, int b);
    void rect(int x, int y, int w, int h, Uint32 color);
    void rect(int x, int y, int w, int h, int r, int g, int b);

    void render(Game* game);
};
4

2 に答える 2

9

ここで前方宣言を使用できます。

#ifndef GRAPHICS_H_ // portable include guards
#define GRAPHICS_H_

#include <SDL.h>

class Game; // forward declaration. Exactly the same as struct Game.

struct Graphics 
{
  // as before
};

#endif

Graphicsの定義を必要としないからですGameGame.hの実装ファイルに含める必要がある可能性が最も高いでしょうGraphics

関連記事を参照してください:前方宣言を使用する場合は?

于 2013-09-15T07:08:08.377 に答える