• Advertisement
Sign in to follow this  

Problem with forward declarations and #includes

This topic is 2130 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

[b][size=8][font=verdana,geneva,sans-serif]---------SOLVED-----------[/font][/size][/b]
[b]Include the headers in your .cpp when you forward declare! [/b][img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]

Hello gamdev,
I am working on a 3D game and I want to give my camera object now a reference to my player (a [b]GameObject[/b]) instead of only a 3D vector [i]- which I used to direct my camera at the player's position-[/i] 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 [b]"Camera.h"[/b]:

[code]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 ==========
[/code]

According to this link: [url="http://www.cplusplus.com/forum/articles/10627/"]http://www.cplusplus...articles/10627/[/url] I don't really need to be that drastic so I tried a forward declaration:

[code]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 ==========
[/code]

Why is it undefined?

So in my first approach my [b]Camera.h[/b] includes [b]GameObject.h[/b] because of a reference to a GameObject object as a function parameter ([i]too drastic[/i]). In [b]GameObject.h[/b] I include [b]Terrain.h[/b] because of pointer to a Terrain object as a function parameter ([i]so my GameObjects stay on the terrain, also too drastic[/i]). In [b]Terrain.h[/b] 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 [b]Camera.h[/b]. ([i]for some culling, and again too drastic![/i])

So I basically have a cyclic dependency.
I tried to forward declare in those files:
[code]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 ==========
[/code]

My main still includes the according headers, and I use these objects like this:
[code]Camera g_Camera;
Terrain* g_Terrain;
std::vector<GameObject*> g_GameObjectList;[/code]
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?

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement