Jump to content

  • Log In with Google      Sign In   
  • Create Account


Problem with forward declarations and #includes


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 Honas   Members   -  Reputation: 101

Like
0Likes
Like

Posted 21 March 2012 - 07:15 AM

---------SOLVED-----------
Include the headers in your .cpp when you forward declare! Posted Image

Hello gamdev,
I am working on a 3D game and I want to give my camera object now a reference to my player (a GameObject) instead of only a 3D vector - which I used to direct my camera at the player's position- so that it can change the orientation of the player. All my headers have inclusion guards and the according source files only include the header. But now I get weird errors when I include "Camera.h":

1>------ Build started: Project: TheGame, Configuration: Debug Win32 ------
1>  Camera.cpp
1>c:\users\jonas\desktop\thegame\terrain.h(19): error C2143: syntax error : missing ';' before '*'
1>c:\users\jonas\desktop\thegame\terrain.h(19): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\jonas\desktop\thegame\terrain.h(19): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\jonas\desktop\thegame\terrain.h(32): error C2061: syntax error : identifier 'Camera'
1>c:\users\jonas\desktop\thegame\terrain.h(52): error C2061: syntax error : identifier 'Camera'
1>c:\users\jonas\desktop\thegame\terrain.h(55): error C2061: syntax error : identifier 'Camera'
1>c:\users\jonas\desktop\thegame\terrain.h(56): error C2061: syntax error : identifier 'Camera'
1>  Terrain.cpp
1>c:\users\jonas\desktop\thegame\gameobject.h(22): error C2061: syntax error : identifier 'Terrain'
1>c:\users\jonas\desktop\thegame\gameobject.h(63): error C2061: syntax error : identifier 'Terrain'
1>c:\users\jonas\desktop\thegame\gameobject.h(65): error C2065: 'terrain' : undeclared identifier
1>  TheGame.cpp
1>c:\users\jonas\desktop\thegame\terrain.h(19): error C2143: syntax error : missing ';' before '*'
1>c:\users\jonas\desktop\thegame\terrain.h(19): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\jonas\desktop\thegame\terrain.h(19): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\jonas\desktop\thegame\terrain.h(32): error C2061: syntax error : identifier 'Camera'
1>c:\users\jonas\desktop\thegame\terrain.h(52): error C2061: syntax error : identifier 'Camera'
1>c:\users\jonas\desktop\thegame\terrain.h(55): error C2061: syntax error : identifier 'Camera'
1>c:\users\jonas\desktop\thegame\terrain.h(56): error C2061: syntax error : identifier 'Camera'
1>c:\users\jonas\desktop\thegame\thegame.cpp(111): error C2660: 'Terrain::draw' : function does not take 3 arguments
1>c:\users\jonas\desktop\thegame\thegame.cpp(249): error C2661: 'Terrain::Terrain' : no overloaded function takes 12 arguments
1>  Generating Code...
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

According to this link: http://www.cplusplus...articles/10627/ I don't really need to be that drastic so I tried a forward declaration:

1>------ Build started: Project: TheGame, Configuration: Debug Win32 ------
1>  Camera.cpp
1>c:\users\jonas\desktop\thegame\camera.cpp(217): error C2027: use of undefined type 'GameObject'
1>		  c:\users\jonas\desktop\thegame\camera.h(4) : see declaration of 'GameObject'
1>c:\users\jonas\desktop\thegame\camera.cpp(217): error C2228: left of '.Position' must have class/struct/union
1>c:\users\jonas\desktop\thegame\camera.cpp(217): error C2228: left of '.x' must have class/struct/union
1>c:\users\jonas\desktop\thegame\camera.cpp(217): error C2027: use of undefined type 'GameObject'
1>		  c:\users\jonas\desktop\thegame\camera.h(4) : see declaration of 'GameObject'
1>c:\users\jonas\desktop\thegame\camera.cpp(217): error C2228: left of '.Position' must have class/struct/union
1>c:\users\jonas\desktop\thegame\camera.cpp(217): error C2228: left of '.y' must have class/struct/union
1>c:\users\jonas\desktop\thegame\camera.cpp(217): error C2027: use of undefined type 'GameObject'
1>		  c:\users\jonas\desktop\thegame\camera.h(4) : see declaration of 'GameObject'
1>c:\users\jonas\desktop\thegame\camera.cpp(217): error C2228: left of '.Position' must have class/struct/union
1>c:\users\jonas\desktop\thegame\camera.cpp(217): error C2228: left of '.z' must have class/struct/union
1>c:\users\jonas\desktop\thegame\camera.cpp(218): error C2027: use of undefined type 'GameObject'
1>		  c:\users\jonas\desktop\thegame\camera.h(4) : see declaration of 'GameObject'
1>c:\users\jonas\desktop\thegame\camera.cpp(218): error C2228: left of '.Position' must have class/struct/union
1>  GameObject.cpp
1>  Terrain.cpp
1>  TheGame.cpp
1>  Generating Code...
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Why is it undefined?

So in my first approach my Camera.h includes GameObject.h because of a reference to a GameObject object as a function parameter (too drastic). In GameObject.h I include Terrain.h because of pointer to a Terrain object as a function parameter (so my GameObjects stay on the terrain, also too drastic). In Terrain.h I pass a camera reference as a function parameter and save a camera pointer in a class contained in the terrain class, so I include Camera.h. (for some culling, and again too drastic!)

So I basically have a cyclic dependency.
I tried to forward declare in those files:
1>------ Build started: Project: TheGame, Configuration: Debug Win32 ------
1>  Camera.cpp
1>c:\users\jonas\desktop\thegame\camera.cpp(217): error C2027: use of undefined type 'GameObject'
1>		  c:\users\jonas\desktop\thegame\camera.h(4) : see declaration of 'GameObject'
1>c:\users\jonas\desktop\thegame\camera.cpp(217): error C2228: left of '.Position' must have class/struct/union
1>c:\users\jonas\desktop\thegame\camera.cpp(217): error C2228: left of '.x' must have class/struct/union
1>c:\users\jonas\desktop\thegame\camera.cpp(217): error C2027: use of undefined type 'GameObject'
1>		  c:\users\jonas\desktop\thegame\camera.h(4) : see declaration of 'GameObject'
1>c:\users\jonas\desktop\thegame\camera.cpp(217): error C2228: left of '.Position' must have class/struct/union
1>c:\users\jonas\desktop\thegame\camera.cpp(217): error C2228: left of '.y' must have class/struct/union
1>c:\users\jonas\desktop\thegame\camera.cpp(217): error C2027: use of undefined type 'GameObject'
1>		  c:\users\jonas\desktop\thegame\camera.h(4) : see declaration of 'GameObject'
1>c:\users\jonas\desktop\thegame\camera.cpp(217): error C2228: left of '.Position' must have class/struct/union
1>c:\users\jonas\desktop\thegame\camera.cpp(217): error C2228: left of '.z' must have class/struct/union
1>c:\users\jonas\desktop\thegame\camera.cpp(218): error C2027: use of undefined type 'GameObject'
1>		  c:\users\jonas\desktop\thegame\camera.h(4) : see declaration of 'GameObject'
1>c:\users\jonas\desktop\thegame\camera.cpp(218): error C2228: left of '.Position' must have class/struct/union
1>  GameObject.cpp
1>c:\users\jonas\desktop\thegame\gameobject.cpp(64): error C2027: use of undefined type 'Terrain'
1>		  c:\users\jonas\desktop\thegame\gameobject.h(5) : see declaration of 'Terrain'
1>c:\users\jonas\desktop\thegame\gameobject.cpp(64): error C2227: left of '->getWidth' must point to class/struct/union/generic type
1>c:\users\jonas\desktop\thegame\gameobject.cpp(65): error C2027: use of undefined type 'Terrain'
1>		  c:\users\jonas\desktop\thegame\gameobject.h(5) : see declaration of 'Terrain'
1>c:\users\jonas\desktop\thegame\gameobject.cpp(65): error C2227: left of '->getDepth' must point to class/struct/union/generic type
1>c:\users\jonas\desktop\thegame\gameobject.cpp(78): error C2027: use of undefined type 'Terrain'
1>		  c:\users\jonas\desktop\thegame\gameobject.h(5) : see declaration of 'Terrain'
1>c:\users\jonas\desktop\thegame\gameobject.cpp(78): error C2227: left of '->getHeight' must point to class/struct/union/generic type
1>c:\users\jonas\desktop\thegame\gameobject.cpp(320): error C2027: use of undefined type 'Terrain'
1>		  c:\users\jonas\desktop\thegame\gameobject.h(5) : see declaration of 'Terrain'
1>c:\users\jonas\desktop\thegame\gameobject.cpp(320): error C2227: left of '->getHeight' must point to class/struct/union/generic type
1>  Terrain.cpp
1>c:\users\jonas\desktop\thegame\terrain.cpp(117): error C2027: use of undefined type 'Camera'
1>		  c:\users\jonas\desktop\thegame\terrain.h(4) : see declaration of 'Camera'
1>c:\users\jonas\desktop\thegame\terrain.cpp(117): error C2227: left of '->pos' must point to class/struct/union/generic type
1>c:\users\jonas\desktop\thegame\terrain.cpp(118): error C2027: use of undefined type 'Camera'
1>		  c:\users\jonas\desktop\thegame\terrain.h(4) : see declaration of 'Camera'
1>c:\users\jonas\desktop\thegame\terrain.cpp(118): error C2227: left of '->pos' must point to class/struct/union/generic type
1>c:\users\jonas\desktop\thegame\terrain.cpp(128): error C2027: use of undefined type 'Camera'
1>		  c:\users\jonas\desktop\thegame\terrain.h(4) : see declaration of 'Camera'
1>c:\users\jonas\desktop\thegame\terrain.cpp(128): error C2228: left of '.isVisible' must have class/struct/union
1>  TheGame.cpp
1>  Generating Code...
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

My main still includes the according headers, and I use these objects like this:
Camera	 g_Camera;	
Terrain*	g_Terrain;	
std::vector<GameObject*> g_GameObjectList;
Do I have to forward declare Terrain and GameObject, too? Well, at the moment it gives me more of the same errors when I do.
Any advice?

Sponsor:

#2 SiCrane   Moderators   -  Reputation: 9560

Like
1Likes
Like

Posted 21 March 2012 - 07:19 AM

Without seeing your actual source code it's hard to say specifically what you're doing wrong. However, some common problems that could exhibit this kind of issue are accidentally reusing symbols for your include guards and not remembering to #include a file in the source file if you use a forward declaration in the header.

#3 wqking   Members   -  Reputation: 756

Like
1Likes
Like

Posted 21 March 2012 - 07:32 AM

With type forwarding, you CAN'T use any object members, or create any object, in the header without the type declaration.

class GameObject;

class MyClass
{
public:
void a() {
p->field = 1; // wrong
}
private:
GameObject o; // wrong
GameObject * p; //correct;
};

You need to either include the header which contains the type declaration, or move the code that using the type to cpp file and include the header in cpp.

http://www.cpgf.org/
cpgf library -- free C++ open source library for reflection, serialization, script binding, callbacks, and meta data for OpenGL Box2D, SFML and Irrlicht.
v1.5.5 was released. Now supports tween and timeline for ease animation.


#4 Honas   Members   -  Reputation: 101

Like
0Likes
Like

Posted 21 March 2012 - 01:55 PM

You guys nailed it, thank you so much! I was unaware of the fact, that I still have to include the header in my cpp file.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS