Include the headers in your .cpp when you forward declare!
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?