This is very strange and I can’t get my head around what is causing it…. So I have two classes, Player and Enemy and a utility static class CollisionMgr.
I have an array of Enemies in the world class, I’d like to have a pointer to it in the Player class for collision detection. But the moment i try to #include the Enemy class in the player one, I get “Player undefined” error in the Enemy one…. I think it has to do with the header inclusions…
#pragma once
#include <string>
#include <vector>
#include "mathlib.h"
#include "md2model.h"
#include "Object3DS.h"
const float PLAYER_FORWARD_SPEED = 60.0f;
const float PLAYER_HEADING_SPEED = 60.0f;
class Player : public GameObject
{
public:
Player(float r);
~Player();
void loadModel(const char *filename);
void drawModel();
void drawBoundingWireframe();
void passVisibleObjects(vector<Object3DS*> &vo);
private:
Entity3D entity;
MD2Model* player;
float modelRot;
float modelPitch;
Vector3 scale;
Vector3 pos;
Vector3 rot;
float forwardSpeed;
float heading;
float radius;
vector<Object3DS*> visibleObjects;
};
And this is the Enemy class
#pragma once
#include <string>
#include <vector>
#include "Player.h"
#include "mathlib.h"
#include "md2model.h"
const float ENEMY_FORWARD_SPEED = 60.0f;
const float ENEMY_HEADING_SPEED = 60.0f;
class Enemy : public GameObject
{
public:
Enemy(Player* p, float r);
~Enemy();
void loadModel(const char *filename);
void drawModel();
void drawBoundingWireframe();
bool checkCollision();
void setPosition(Vector3 pos);
void setRotation(Vector3 r);
void setScale(Vector3 r);
void setSpeed(float s);
void setHeading(float h);
float getForwardSpeed();
float getHeading();
void rotateModel(float dx, float dy);
void resetRotation();
void setAnimation(char *filename);
Vector3 getPosition();
void moveEnemy(float elapsedTimeSec);
Entity3D entity;
void passVisibleObjects(vector<Object3DS*> &vo);
private:
MD2Model* enemy;
Player* player;
float modelRot;
float modelPitch;
Vector3 scale;
Vector3 pos;
Vector3 rot;
float forwardSpeed;
float heading;
float radius;
vector<Object3DS*> visibleObjects;
};
In the source files i include this static class with some collision functions:
#pragma once
#include <vector>
#include "mathlib.h"
#include "Object3DS.h"
#include "Enemy.h"
class CollisionMgr{
public:
static vector<Object3DS*> findVisibleObjects(BoundingBox &viewingVolume, vector<Object3DS> &gameObjects, vector<Object3DS*> &visibleObjects);
static vector<Enemy*> findVisibleEnemies(BoundingBox &viewingVolume, vector<Enemy> &enemies, vector<Enemy*> &visibleEnemies);
static bool checkCollision(const Vector3 pos, const Vector3 forward, float radius, const vector<Object3DS*> &vo);
static double intersectRaySphere(Vector3 rO, Vector3 rV, Vector3 sO, double sR);
};
All of these compile ok like this, but the moment I try to add #include “Enemy” in Player I get error C2061: syntax error : identifier ‘Player’ inside the Enemy class….or if I include successfully Enemy and then try to include CollisionMgr…. there’s some kind of conflict between them two and the definition of Player but I cant understand how to resolve it….
Am using VS2010 as my compiler..
Any help would be appreciated!
I tried re arranging the include statements but I really don’t get what is causing this?
This is a case of a circular dependency. You will need to properly forward declare
PlayerandEnemyand you will not be able to use their definitions in the opposite header file. You can use them in the implementation file. Here’s an example:Player.h
Enemy.h
Including the header file in both locations will cause an error (like you are seeing):
Player.h
Enemy.h
What is needed here is a “forward declaration” for
PlayerandEnemywhere appropriate, like this:Player.h
Enemy.h
Then in the implementation files you can
#includethe appropriate headers to actually get the definitions of the objects in your compilation unit.