Reputation: 909
I have a header file named Application.h where I include a header named CollisionHandler.h. CollisionHandler includes Application.h, so I get a too many includes error when compiling. To solve this I've put the CollisionHandler include between header guards, like this:
#ifndef COLLISION_HANDLER_INCLUDED_H
#define COLLISION_HANDLER_INCLUDED_H
#include "CollisionHandler.h"
#endif
but when I try to use a object of type CollisionHandler (this class is defined in CollisionHandler.h, between header guards) as a member variable of the Application class (which is also defined between header guards in Application.h), I get this error repeated for every file that includes Application.h (5 times or so):
1>c:\users\aitor\documents\visual studio 2008\projects\copter\copter\application.h(19) : error C2143: syntax error : missing ';' before '*'
1>c:\users\aitor\documents\visual studio 2008\projects\copter\copter\application.h(19) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\aitor\documents\visual studio 2008\projects\copter\copter\application.h(19) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
The line 19 is the line where I declare the CollisionHandler object as a member variable.
Here is the code in Application.h (With the relevant lines idented):
#include "Header.h"
#include <stdio.h>
#include "GameCharter.h"
#include <vector>
#include <boost/shared_ptr.hpp>
#ifndef COLLISION_HANDLER_INCLUDED_H //Here I include
#define COLLISION_HANDLER_INCLUDED_H //the collision
#include "CollisionHandler.h" //handler header
#endif
using namespace std;
#ifndef APPLICATION_DEFINED_H
#define APPLICATION_DEFINED_H
class LimitsManager;
class ObstacleManager;
class Application
{
public:
CollisionHandler *handler; //Here I declare the CollisionHandler
ObstacleManager *obstacleManager;
LimitsManager *limitsManager;
vector<boost::shared_ptr<GameChar> > characters;
vector<int> idsToRemove;
void gameLoop();
Application();
bool idsNeedUpdate;
bool objectsNeedToRemove;
};
#endif
And this is the code for CollisionHandler.h:
#include "Header.h"
#include "Application.h"
#include "GameCharter.h"
#include "LimitObstacle.h"
#ifndef COLLISION_HANDLER_H
#define COLLISION_HANDLER_H
class CollisionHandler
{
Application *app;
public:
void handleCollisions();
CollisionHandler()
{
}
CollisionHandler(Application *app);
bool collidersAreCollidingGeneral(GameChar* char1,GameChar* char2);
bool collidersAreCollidingSecondary(GameChar* char1,GameChar* char2);
};
#endif
Also, if I use class CollisionHandler;
in Application.h and then include CollisionHandler.h in the cpp file, it works
Upvotes: 1
Views: 2477
Reputation: 21
all you need to do is add/write
pragma once
at the top of each header file
Upvotes: 2
Reputation: 258578
Including headers cyclically is wrong, even with include guards.
Try to remove the dependency by using forward declarations where possible.
If you can't use forward declarations, your design is flawed.
Upvotes: 2