Blaxx
Blaxx

Reputation: 225

C++, #include problem!

My Includes are in a header called "stats.h"

I have 2 classes A and B.

A requires the header of B and A the header of B

When I Try to compile, I get about 50 errors like this one

identifier 'Player'
int assumed
; before * missing

Any ideads on how to fix this? :(

(Here the relevant code)

CLASS A - PLAYER

#include "stats.h"

class Player
{
public:
Player(int x, int y);

Texture *texture;

int x;
int y;

float rotation;

void Update(double elapsedTime);
void Draw(Sprite *sprite);

private:
Weapon *weapon;
};

CLASS B - WEAPON

#include "stats.h"

class Weapon // **CLASS B**
{
public:
double interval;

Weapon(Player *player);

void Update(double elapsedTime);
void Draw(Sprite *sprite);

protected:

private:
Player *player;
};

STATS.H

#include "Player.hpp"
#include "Weapon.hpp"

Upvotes: 0

Views: 325

Answers (2)

user2100815
user2100815

Reputation:

Although using forward declarations as others have suggested will work, your real problem is with the design. Mutual dependencies between classes like this are almost always a sign that something is wrong with the design. In this case, I would really want to know if Weapon needs to know about Player.

Upvotes: 0

Kiril Kirov
Kiril Kirov

Reputation: 38193

You don't have implementation in your headers, so you could pass with forward declarations and include the headers only in the cpp files.


For example - before class Player, put forward declaration of class Weapon:

//vvvvvvvvvvv
class Weapon;

class Player
{
public:
    Player(int x, int y);
    /* .. */
};

and remove the #include "stats.h". The same for class Weapon

Upvotes: 1

Related Questions