如何正确使用前向声明?

How to use forward declaration correctly?

我有两个文件: game.h

#include <ctime>
#include "renderSystem.h"
#include "gameObject.h"
#include "gameObjectType.h"

const int kObjectsCountMax = 1024;


class GameObject;


class Game{
public:
    Game();

    void setupSystem();
    void initialize();
    bool frame();
    void shutdown();

    GameObject* createObject(GameObjectType type, float x, float y);
    void destroyObject(GameObject* object);
    GameObject* checkIntersects(float x, float y, float width, float height, GameObject* 
    exceptObject);
    bool moveObjectTo(GameObject* object, float x, float y);
    int getObjectCount(GameObjectType type);

private:
    void render();
    void update(float dt);

private:
    bool m_isGameActive;
    clock_t m_clockLastFrame;

    RenderSystem m_renderSystem;

    GameObject* m_objects[kObjectsCountMax]{};

    GameObject* m_base;

    GameObject* m_player1;
    GameObject* m_player2;
};
#include "game.cpp"

还有第二个文件 - gameObject.h

#pragma once

#include "renderSystem.h"
#include "direction.h"
#include "gameObjectType.h"



class Game;

class GameObject{
public:
    GameObject();
    virtual ~GameObject();

    virtual void render(RenderSystem* rs);
    virtual void update(float dt);

    virtual void intersect(GameObject* object);

    GameObjectType getType(){ return m_type; }

    void setGame(Game* game){ m_game = game;}

    void setX(float x){ m_x = x;}
    float getX(){ return m_x;}

    void setY(float y){ m_y = y;}
    float getY(){ return m_y;}

    void setXSpeed(float xSpeed){ m_xSpeed = xSpeed;}
    float getXSpeed(){ return m_xSpeed;}

    void setYSpeed(float ySpeed){ m_ySpeed = ySpeed;}
    float getYSpeed(){ return m_ySpeed;}

    void setWidth(int width){ m_width = width;}
    int getWidth(){ return m_width;}

    void setHeight(int height){ m_height = height;}
    int getHeight(){ return m_height;}

    void setHealth(int health){ m_health = health;}
    int getHealth(){ return m_health;}

    void setDestroyAfterDeath( bool destroyAfterDeath){ m_destroyAfterDeath = 
    destroyAfterDeath;}
    bool getDestroyAfterDeath(){ return m_destroyAfterDeath;}

    void setInvolnerable( bool involnerable){ m_involnerable = involnerable;}
    bool getInvolnerable(){ return m_involnerable;}

    void setPhysical( bool physical){ m_physical = physical;}
    bool getPhysical(){ return m_physical;}

    void setDirection(Direction direction){ m_direction = direction;}
    Direction getDirection(){return m_direction;}

    void doDamage(int damage);

protected:
    Game* m_game;
    GameObjectType m_type;

    float m_x;
    float m_y;
    float m_xSpeed;
    float m_ySpeed;

    int m_height;
    int m_width;

    int m_health;
    bool m_destroyAfterDeath;
    bool m_involnerable;

    bool m_physical;

    Direction m_direction;

};
#include "gameObject.cpp"

当我尝试使用游戏 class 的方法(即 m_game-> moveObjectTo (this, m_x, newY),见下文)。错误:不完整类型 'class Game' 的无效使用。 我在 gameObject.h 中转发了它,但是 t understand why compiler doesn 在 gameObject.cpp 中看不到它。

文件gameObject.cpp:

#include "gameObject.h"
#include "level.h"
#include "game.h"



GameObject::GameObject() {
    m_game = 0;
    m_type = GameObjectType_None;

    m_x = 0.0;
    m_y = 0.0;
    m_xSpeed = 0.0;
    m_ySpeed = 0.0;

    m_width = 1;
    m_height = 1;

    m_health = 1;
    m_destroyAfterDeath = true;
    m_involnerable = false;

    m_physical = true;

    m_direction = Direction_Up;
}

void GameObject::update(float dt) {
    int oldRow = int(m_y);
    int oldColumn = int(m_x);

    float newY = m_y + m_ySpeed* dt;
    float newX = m_x + m_xSpeed* dt;

    int newRow = int(newY);
    int newColumn = int(newX);

    if (oldColumn != newColumn){
        //Problem is here
        m_game->moveObjectTo(this, newX, m_y);
    }
    else{
        m_x = newX;
    }
    if (oldRow != newRow){
         //Problem is here
         m_game->moveObjectTo(this, m_x, newY);
    }
    else{
        m_y = newY;
    }

}

也许我搞砸了#include 或直接用前向声明。请帮助理解我做错了什么。

从 game.h 中删除#include“gameObject.h”。谢谢

好吧,您在 game.h 中包含 game.cpp,这无论如何都不理想 - 在 h 文件中包含 cpp 文件没有意义。如果您已经包含 gameObject.cpp,我不明白为什么要在 game.h 中转发声明 GameObject。这些令人困惑但不会导致错误。

它不起作用的原因是因为您在 gameObject.h 中包含 gameObject.cpp,因为 gameObject.cpp 包含 game.h 导致循环依赖。