• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Honas

Problem with forward declarations and #includes

3 posts in this topic

[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?
0

Share this post


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

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.
1

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.
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0