Class has not been declared

I'm currently working on a small text game for a class and I've hit a bit of a bump. When i try to compile, I get an error saying that one of the classes has not been declared. I figured out that it's because two classes are included in each other, but I can't figure out how to fix it...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#ifndef ENEMY_H
#define ENEMY_H

#include <iostream>
#include "player.h"
using namespace std;

class Enemy
{
public:
    ...
private:
    ...
};

#endif // ENEMY_H 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#ifndef PLAYER_H
#define PLAYER_H

#include <iostream>
#include <map>
#include "enemy.h"
using namespace std;

class Player
{
public:
    ...
private:
    ...
};

#endif // PLAYER_H 
I believe I have ran into this issue before myself.

The solution I found helpful for my own projects:
The way I fixed it was only including the enemy header and the player header files into the main cpp file.

The reason why it might be doing that:
I think what is going on is that it's trying to add the player class into the enemy class and then adding it into the player class again and then it has two definitions of the player class and the complier doesn't know which one it needs to use.

I hope this helps.

- Hirokachi
This article explains the problem, along with a lot of other useful information:
http://www.cplusplus.com/forum/articles/10627/
Topic archived. No new replies allowed.