I have two files: 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"
And a second file - 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"
The error appears in file "gameObject.cpp" when I try to use a method of the Game class (namely m_game-> moveObjectTo (this, m_x, newY), see below). error: invalid use of incomplete type 'class Game'.
I forward declared it in gameObject.h, but dont understand why compiler doesn
t see it in gameObject.cpp.
File 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;
}
}
Perhaps I messed up with #include or directly with forward declaration. Please help to understand what I did wrong.
Well, you include game.cpp in game.h which is not ideal by any means - it doesn't make sense to include cpp files in an h file. And I don't see why you forward declare GameObject in game.h if you already include gameObject.cpp. Those are confusing but not cause the error.
The reason it doesn't work is because you include gameObject.cpp in gameObject.h, as gameObject.cpp includes game.h resulting in circular dependency.