1

I'm writing a game in c++ in microsoft visual studio 2010, yesterday I wrote a pong game and everything was fine but now the compiler telling me that there is a lot of errors for example:

1>w:\c++\planet escape\planet escape\room.h(25): error C2061: syntax error : identifier 'WorldMap'

And here is the Room.h file:

#pragma once

#include <allegro5/allegro.h>
#include <vector>
#include "Entity.h"
#include "WorldMap.h"
#include "Link.h"

#define ROOM_W 20
#define ROOM_H 20

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

When in code there is no mistakes and it sees all the classes fine. So what can cause such mistake?

EDIT: here is the WorldMap.h

#pragma once

#include <allegro5/allegro.h>
#include "Room.h"
#include "Player.h"

#define WORLD_W 10
#define WORLD_H 10

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

If when I'm runing it he cant see it then why he see it when coding?

11
  • 3
    Are you sure WorldMap.h defines WorldMap type? compiler is telling you it cannot identify the type WorldMap. Commented Aug 20, 2011 at 15:44
  • I think we'd need to see the WorldMap.h file to answer this question. Commented Aug 20, 2011 at 15:46
  • Since you're only using a WorldMap* you don't actually need to pull in the whole definition anyway - you can just add a class WorldMap; before your class Room {..}; declaration. You'll need the full class before you actually use it, though. Commented Aug 20, 2011 at 15:46
  • @Rup: Bad advice, This will just hide the problem in this case. If WorldMap.h does not define WorldMap then OP might get rid of this error, but will be eventually stuck at a point where, forward declaration won't work. Commented Aug 20, 2011 at 15:49
  • 2
    You have circular includes. Don't do that. Commented Aug 20, 2011 at 15:54

1 Answer 1

5

You have circular includes. Suppose you are compiling a file that has a #include "WorldMap.h" as the first applicable #include statement. The file WorldMap.h has that #include "Room.h" that is going to cause a lot of trouble. The problems start in earnest in Room.h at the #include "WorldMap.h" statement. That #include has no effect thanks to the #pragma once in WorldMap.h. When the compiler gets to the point of processing the main body of Room.h, the class WorldMap is neither defined nor declared.

Addendum
The solution is to get rid of those extraneous #include statements. The file WorldMap.h does not need to #include either of Room.h or Player.h. It instead needs to make forward declarations of the classes Room and Player. Similarly, you don't need all those #include statements in Room.h either.

It is in general a good idea to use forward declarations of types in your headers instead of including the file that defines the types. If the code in the header does not need to know details of the type in question, just use a forward declaration. Do not #include the header that defines the type.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.