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?
You have circular includes. Suppose you are compiling a file that has a
#include "WorldMap.h"as the first applicable#includestatement. The fileWorldMap.hhas that#include "Room.h"that is going to cause a lot of trouble. The problems start in earnest inRoom.hat the#include "WorldMap.h"statement. That#includehas no effect thanks to the#pragma onceinWorldMap.h. When the compiler gets to the point of processing the main body ofRoom.h, the classWorldMapis neither defined nor declared.Addendum
The solution is to get rid of those extraneous
#includestatements. The fileWorldMap.hdoes not need to#includeeither ofRoom.horPlayer.h. It instead needs to make forward declarations of the classesRoomandPlayer. Similarly, you don’t need all those#includestatements inRoom.heither.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
#includethe header that defines the type.