• 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
Concentrate

Can someone explain the problem with this compilation error!

6 posts in this topic

//Piece.h[CODE]


#ifndef TestProject_C___Piece_h
#define TestProject_C___Piece_h

#include "Board.h"

namespace Foo {
namespace detail{
struct PieceEnums{
enum PieceID{ ID1, ID2 };
};
}

class Board;

class Piece{
public:
void foo(Board& b)const;
};
}
#endif
[/CODE]
//board.h

[CODE]
#ifndef TestProject_C___Board_h
#define TestProject_C___Board_h

#include "Piece.h"


namespace Foo {
class Piece;

class Board{
//ERROR OCCURS HERE
bool isOccupied(int x, int y,const detail::PieceEnums::PieceID pid)const;
};
}

#endif[/CODE]


And the error is 'Use of undeclared identifier detail'

Note that this is across multiple files, so maybe its a problem with linkage?


So I found a solution and its basically to put the namespace detail in a separate file, possibly details.h

I'm guessing that the problem is with the circular reference or something. Can someone explain why the above does not compile.
0

Share this post


Link to post
Share on other sites
It compiles fine for me. Are you including "piece.h" before "board.h" in a other files? If so, try including "board.h" first and see what happens.
1

Share this post


Link to post
Share on other sites
[quote name='RulerOfNothing' timestamp='1326596250' post='4902845']
Actually, I think it might be because you put the "detail" namespace inside the "Foo" namespace.
[/quote]

I tried making it its own namespace, that is not having it nested and still get the same problem. The problem was exactly what Labouts said! But shouldn't piece.h be included before board.h? Also I guess putting the detail namespace in a different file is the best solution since we won't have to worry about making sure a.h gets included before b.h and so on. Thanks.
0

Share this post


Link to post
Share on other sites
I think the compiler goes through piece.h first, and defines your header guard (ifndef and define statements) from where it tries to include board.h which tries coming back to piece.h, except the header guard is already defined so it effectively skips over the #include "Piece.h" statement. The problem is that now PieceEnums is defined after Board alas, it says that it's undefined. Although that depends on which header is included first. I'm pretty sure there is a name for this type of inclusion (where two headers try including each other).

I find that most of the time I can fix this with forward declarations while including the headers in .cpp files. As a general rule .cpp files should never be included in headers so that allows the inclusion guards to properly include the headers no sooner than they need to appear. This solution, however, is a little limited, as the headers must declare the objects as pointers or references because if they don't, the compiler will have to calculate the memory necessary to store the object by value and it can't determine it without a full declaration of the class. I don't think it's that big of an inconvenience.

Of course, that only MAY be true (if I remember what I read correctly). Knowing C++, I'm pretty sure I missed something.
1

Share this post


Link to post
Share on other sites
The problem is that piece.h and board.h depend on each other (by including each other). Such a dependency can never be corrected resolved, so you must change your code.

Remember that #include is just textual substitution by the preprocessor, and that the compiler builds each translation unit in isolation. A translation unit is originated from a source file (.cpp) -- header files are typically never explicitly compiled, their code is first preprocessed into a source file which is then compiled. In your build output, you should be able to see the actual .c or .cpp file the compiler is trying to build when it reports this error.

That source file probably includes piece.h, directly or indirectly. It probably does so before including board.h, directly or indirectly. This means that the preprocessor would have first evaluated the #include for piece.h, and then during the evaluation of piece.h, evaluated board.h. During evaluation of board.h, piece.h would be evaluated but because of the include guards in piece.h, no text was added to the translation unit.

Thus, the file preprocessed file (as seen by the compiler) probably looks like this:

[CODE]
namespace Foo {
class Piece;

class Board{
//ERROR OCCURS HERE
bool isOccupied(int x, int y,const detail::PieceEnums::PieceID pid)const;
};
}

namespace Foo {
namespace detail{
struct PieceEnums{
enum PieceID{ ID1, ID2 };
};
}

class Board;

class Piece{
public:
void foo(Board& b)const;
};
}
#endif
[/CODE]

You can see, then, why the compiler complains that "detail" is unknown -- it hasn't been seen yet at the point the error occurs.

You need to break the dependency between the files to fix the problem. From the code you've provided, board.h doesn't need anything from piece.h except the enum, which you could pull out into its own header, for example. A forward declaration of Piece (or Board) would allow you to use Piece (or Board) as pointers or references or in other ways incomplete types are permissible as well, without requiring the #include.
1

Share this post


Link to post
Share on other sites
[quote name='Josh Petrie' timestamp='1326609470' post='4902886']
The problem is that piece.h and board.h depend on each other (by including each other). Such a dependency can never be corrected resolved, so you must change your code.

Remember that #include is just textual substitution by the preprocessor, and that the compiler builds each translation unit in isolation. A translation unit is originated from a source file (.cpp) -- header files are typically never explicitly compiled, their code is first preprocessed into a source file which is then compiled. In your build output, you should be able to see the actual .c or .cpp file the compiler is trying to build when it reports this error.

That source file probably includes piece.h, directly or indirectly. It probably does so before including board.h, directly or indirectly. This means that the preprocessor would have first evaluated the #include for piece.h, and then during the evaluation of piece.h, evaluated board.h. During evaluation of board.h, piece.h would be evaluated but because of the include guards in piece.h, no text was added to the translation unit.

Thus, the file preprocessed file (as seen by the compiler) probably looks like this:

[CODE]
namespace Foo {
class Piece;

class Board{
//ERROR OCCURS HERE
bool isOccupied(int x, int y,const detail::PieceEnums::PieceID pid)const;
};
}

namespace Foo {
namespace detail{
struct PieceEnums{
enum PieceID{ ID1, ID2 };
};
}

class Board;

class Piece{
public:
void foo(Board& b)const;
};
}
#endif
[/CODE]

You can see, then, why the compiler complains that "detail" is unknown -- it hasn't been seen yet at the point the error occurs.

You need to break the dependency between the files to fix the problem. From the code you've provided, board.h doesn't need anything from piece.h except the enum, which you could pull out into its own header, for example. A forward declaration of Piece (or Board) would allow you to use Piece (or Board) as pointers or references or in other ways incomplete types are permissible as well, without requiring the #include.
[/quote]

I see. Thanks for the great explanation! Another option I thought was to try to forward declare the enum through the Piece class. But I don't think it is allowed in the current standard right? For example,
[code]
class Piece;
enum Piece::PieceId;

class Board{...};
[/code]

That's not allowed correct? Unless I'm getting the syntax incorrect? Will it be allowed in the coming C++?

And I ended up declaring namespace detail in its own file, which becomes easier to handle.
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