Skip to main content
gamedev.net
🔒 Locked

Circular includes problems with c++

Started by AluminX 7 replies 1k views
Original Post
AluminX
AluminX
Hello all,
I'm having trouble with including files. I tried to include files after #ifndef #define. but it still throws me errors when i try to compile.

This is the setup:
Bullet.h Class
#include "Entity.h"#include "Turret.h"#ifndef Bullet_H#define Bullet_Hclass Bullet : public Entity {private:        private stuff...        Turret *ownerpublic:	method definitions...};#endif


Now the Turret.h Class
#include "Entity.h"#include "Bullet.h"#ifndef Tower_H#define Tower_Hclass Tower : public Entity {private:public:	Bullet *shoot();};#endif


This kind of setup throws a "fatal error C1014: too many include files : depth = 1024"

If I move the includes inside #ifndef than i get all kinds of syntax errors.

Thank you in advance.
Adrian
alvaro
alvaro
The first line of a header file should be #ifndef SOMETHINGSOMETHING'. Don't put any #include' directives before that. Those include guards are there precisely to avoid this problem.
incin
incin
For circularly referencing things like this, you will need forward declarations. i.e. instead of including Bullet.h, do:

class Bullet;
AluminX
AluminX
Umm i did that but as i mention before now the compiler is throwing syntax errors left and right.

do i need to do anything special in the source file?
In my Bullet.cpp all i have is this.

#include "Bullet.h"Bullet::Bullet(Entity *owner) : 	Entity(2.0, 2.0), 	power(15), 	belongs(owner),	_speed(10.0/1000){		//code here}void Bullet::move() {	//code here}


I don't get why this has to be so damn complicated.
alvaro
alvaro
The syntax errors may not mean anything to you, but they might mean something to us. It's hard to help you if you don't post more details.

EDIT: You should probably put #include "Turret.h"' in Bullet.cpp. The header file only needs to know that Turret' is a class, because all you are doing is declaring a pointer to it. But presumably in Bullet.cpp you need to know the details of what a `Turret' is.

I don't think this its very complicated, but you have to get used to doing things a certain way. With practice, it feels quite natural.

Endurion
Endurion
To sum the previous posters up, change your code to this. Also note, that you have to include Turret.h in Bullet.cpp and Bullet.h in Tower.cpp then (to have the full class layout known to the compiler).

Bullet.h Class
* Moved inclusion guards on top (#include inside the inclusion guards)
* Forward declared class Turret

#ifndef Bullet_H#define Bullet_H#include "Entity.h"// forward declare Turret;class Turret;class Bullet : public Entity {private:        private stuff...        Turret *ownerpublic:	method definitions...};#endif


Tower.h
* Moved inclusion guards on top (#include inside the inclusion guards)
* Forward declared class Bullet

#ifndef Tower_H#define Tower_H#include "Entity.h"class Bullet;class Tower : public Entity {private:public:	Bullet *shoot();};#endif



Note that forward declaration only works if you use pointers/references of a class. As soon as you need the real class (member, access members) you have to include the declaring header for real.

Also, is there a reason, why one header says Turret, the other Tower?
Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>
Burnhard
Burnhard
Yes, use forward declarations (include the class in the .cpp file, not the header). If you're using VC, you can use "#pragma once" instead of the #define guard too.
rip-off
rip-off
Read this article. It will come in handy for some of the other issues you are likely to come up against.
AluminX
AluminX
Thank you everyone for the quick replies. Using the forward declaration and including the header file in the .cpp solution seems to be working.
I guess i just need to spend some time with the language and figure out the ins and outs.

Thanks again.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.