【发布时间】:2021-05-15 06:53:19
【问题描述】:
我有两个文件: 游戏.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"
当我尝试使用 Game 类的方法(即 m_game-> moveObjectTo (this, m_x, newY),见下文)时,错误出现在文件“gameObject.cpp”中。错误:对不完整类型“类游戏”的无效使用。
我在 gameObject.h 中转发声明它,但 dont understand why compiler doesnt 在 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",以及.cpp 文件的所有#inclusions。
标签: c++ forward-declaration incomplete-type