Jump to content
• Advertisement

Public Group

# Can someone explain the problem with this compilation error!

This topic is 2838 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

//Piece.h #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 
//board.h

 #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

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.

#### Share this post

##### Share on other sites
Advertisement
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.

#### Share this post

##### Share on other sites
Actually, I think it might be because you put the "detail" namespace inside the "Foo" namespace.

#### Share this post

##### Share on other sites

Actually, I think it might be because you put the "detail" namespace inside the "Foo" namespace.

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.

#### Share this 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.

#### Share this 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:

 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 

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.

#### Share this 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:

 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 

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.

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,
 class Piece; enum Piece::PieceId; class Board{...}; 

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.

#### Share this post

##### Share on other sites

• Advertisement
• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• Advertisement

• ### Popular Now

• 15
• 11
• 9
• 11
• 15
• Advertisement
×

## Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!