Skip to main content
GameDev.net gamedev.net
🔒 Locked 🎮 Unity

include problem

Started by ekba89 Nov 27, 2011 at 4:12 AM 14 replies 1.9k views
Original Post
ekba89
ekba89
I've enemyship class which has includes like below and when i try to include it in another .h file i get a lot of errors and when i include it in .cpp file i can't use local variables of class. I have no idea what is the problem so i don't know which code parts should i add.


#pragma once
#include <glm.hpp>
#include <gtc/quaternion.hpp>
#include <gtc/matrix_transform.hpp>
#include <gtx/quaternion.hpp>
#include "BoundingSphere.h"
#include "Timer.h"
#include "helper.h"
#include <vector>
#include "Bullet.h"
#include "Ship.h"
ekba89
ekba89
ok i started to read it now and here is the error log


1> AsteroidField.cpp
1>c:\users\ekinsan\documents\visual studio 2010\projects\glproject\glproject\ship.h(20): error C2061: syntax error : identifier 'AsteroidField'
1>c:\users\ekinsan\documents\visual studio 2010\projects\glproject\glproject\ship.h(23): error C2061: syntax error : identifier 'AsteroidField'
1>c:\users\ekinsan\documents\visual studio 2010\projects\glproject\glproject\enemyship.h(47): error C2061: syntax error : identifier 'AsteroidField'
1>c:\users\ekinsan\documents\visual studio 2010\projects\glproject\glproject\enemyship.h(50): error C2061: syntax error : identifier 'AsteroidField'
1> Generating Code...
1> Compiling...
1> Enemies.cpp
1> Generating Code...
1> Compiling...
1> main.cpp
1>c:\users\ekinsan\documents\visual studio 2010\projects\glproject\glproject\enemyship.h(47): error C2061: syntax error : identifier 'Ship'
1>c:\users\ekinsan\documents\visual studio 2010\projects\glproject\glproject\enemyship.h(50): error C2061: syntax error : identifier 'AsteroidField'
1> SpacePart.cpp
1>c:\users\ekinsan\documents\visual studio 2010\projects\glproject\glproject\space.h(17): error C2143: syntax error : missing ';' before '*'
1>c:\users\ekinsan\documents\visual studio 2010\projects\glproject\glproject\space.h(17): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\ekinsan\documents\visual studio 2010\projects\glproject\glproject\space.h(17): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1> Space.cpp
1>c:\users\ekinsan\documents\visual studio 2010\projects\glproject\glproject\asteroidfield.h(21): error C2061: syntax error : identifier 'Space'
1>c:\users\ekinsan\documents\visual studio 2010\projects\glproject\glproject\asteroidfield.h(34): error C2143: syntax error : missing ';' before '*'
1>c:\users\ekinsan\documents\visual studio 2010\projects\glproject\glproject\asteroidfield.h(34): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\ekinsan\documents\visual studio 2010\projects\glproject\glproject\asteroidfield.h(34): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1> Ship.cpp
1>c:\users\ekinsan\documents\visual studio 2010\projects\glproject\glproject\enemyship.h(47): error C2061: syntax error : identifier 'Ship'
1>c:\users\ekinsan\documents\visual studio 2010\projects\glproject\glproject\enemyship.h(50): error C2061: syntax error : identifier 'AsteroidField'
1> EnemyShip.cpp
1> Generating Code...
1> Skipping... (no relevant changes detected)
1> Asteroid.cpp
TheUnbeliever
TheUnbeliever
I'm guessing you've missed a semicolon after a class somewhere.
[TheUnbeliever]
ekba89
ekba89
But then shouldn't it give compile errors even if i don't include it anywhere
ekba89
ekba89
Ok i'm adding all of the includes from .h files that are named in the error log. Also i read the article and i don't think i have one of those problems but maybe i can't see it.
When i remove comment tag from EnemyShip include in SpacePart.h it starts to give errors.


//SpacePart.h
#pragma once
#include <list>
#include "BoundingBox.h"
#include "Asteroid.h"
//#include "EnemyShip.h"



//EnemyShip.h
#pragma once
#include <glm.hpp>
#include <gtx/quaternion.hpp>
#include "BoundingSphere.h"
#include "Timer.h"
#include "helper.h"
#include <vector>
#include "Bullet.h"
#include "Ship.h"
#include "AsteroidField.h"



//Enemies.h
#pragma once
#include "helper.h"
#include <glm.hpp>
#include "Ship.h"
#include "EnemyShip.h"
#include "Renderer.h"
#include "Space.h"
#include "AsteroidField.h"



//Ship.h
#pragma once
#include "helper.h"
#include "Mesh.h"
#include <glm.hpp>
#include <gtc/quaternion.hpp>
#include "BoundingSphere.h"
#include "ShipController.h"
#include "Renderer.h"
#include <vector>
#include "AsteroidField.h"
#include "Timer.h"
#include "Bullet.h"



//AsteroidField.h
#pragma once
#include <glm.hpp>
#include <gtx/transform.hpp>
#include <vector>
#include <gl/glut.h>
#include "Renderer.h"
#include "Mesh.h"
#include "helper.h"
#include "BoundingSphere.h"
#include "Asteroid.h"
#include "Space.h"



//Space.h
#pragma once
#include "BoundingBox.h"
#include "BoundingSphere.h"
#include <glm.hpp>
#include "ParticleEngine.h"
#include "SpacePart.h"
SiCrane
SiCrane
It sounds like a cyclical dependency to me (problem 2 in the article). You don't give enough information to know for sure, but this happens if any of your headers directly or indirectly includes a header that includes itself. SpacePart.h includes both BoundingBox.h and Asteroid.h. If either of those includes SpacePart.h or any of the files that either of those includes SpacePart.h or any of the files that any of those includes SpacePart.h, etc. then you have a cyclical dependency issue.
TheUnbeliever
TheUnbeliever
But then shouldn't it give compile errors even if i don't include it anywhere


Visual Studio (and other build systems I've used) usually won't touch a header unless it's been included: they just follow convention and compile only 'source' (e.g. cpp) files. Doesn't mean my guess was right, though.
[TheUnbeliever]
ekba89
ekba89

It sounds like a cyclical dependency to me (problem 2 in the article). You don't give enough information to know for sure, but this happens if any of your headers directly or indirectly includes a header that includes itself. SpacePart.h includes both BoundingBox.h and Asteroid.h. If either of those includes SpacePart.h or any of the files that either of those includes SpacePart.h or any of the files that any of those includes SpacePart.h, etc. then you have a cyclical dependency issue.


Thanks. As you said there was a cyclical dependency problem and i solved it with forward declaration and including header file in cpp file when i needed to use it's functions. But my question is, is there a problem with including header file in cpp?
SiCrane
SiCrane
I'm not sure I understand your question. It's perfectly normal to include a header file in a .cpp file.
Cornstalks
Cornstalks

But my question is, is there a problem with including header file in cpp?


That's what you're supposed to do. Header files are meant to be included in source files (and header files if necessary).
DarklyDreaming
DarklyDreaming
Header schmeader. To C++, it's all the same. It's because of the "dumb includes" that you are having problems (if it is circular dependency, that is). Basically, C++ "injects" the code from the header when you include it - so, headers are mostly used for readability rather than anything else.

.cpp and .h do not carry some sort of magic difference or inherent meaning - they both just contain C++ code.
"I will personally burn everything I've made to the fucking ground if I think I can catch them in the flames."
~ Gabe

"I don't mean to rush you but you are keeping two civilizations waiting!"
~ Cavil, BSG.
"If it's really important to you that other people follow your True Brace Style,
ekba89
ekba89
I know both can contain c++ code but how come when i make forward declaration for my Space class include it in cpp file it works and when i just include it in header file it makes circular dependency. It is hard to tell so let me show you the code . Also as far as i know #pragma once prevents circular dependency am I wrong?

this works

//AsteroidField.h
#pragma once
#include <glm.hpp>
#include <gtx/transform.hpp>
#include <vector>
#include <gl/glut.h>
#include "Renderer.h"
#include "Mesh.h"
#include "helper.h"
#include "BoundingSphere.h"
#include "Asteroid.h"

class Space;

//AsteroidField.cpp
#include "AsteroidField.h"
#include "Space.h"


this doesn't

//AsteroidField.h
#pragma once
#include <glm.hpp>
#include <gtx/transform.hpp>
#include <vector>
#include <gl/glut.h>
#include "Renderer.h"
#include "Mesh.h"
#include "helper.h"
#include "BoundingSphere.h"
#include "Asteroid.h"
#include "Space.h"

//AsteroidField.cpp
#include "AsteroidField.h"
SiCrane
SiCrane

Also as far as i know #pragma once prevents circular dependency am I wrong?

No. #pragma once prevents double inclusion, which is a different problem.
ekba89
ekba89
Ok i got it. Thanks everybody for helping.

Topic Locked

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

Sign in to reply to this topic.