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"
#defin开发者_如何学Ce 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?
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.
精彩评论