• Create Account

# Can someone explain the problem with this compilation error!

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

6 replies to this topic

### #1D.Chhetri  Members   -  Reputation: 180

Like
0Likes
Like

Posted 14 January 2012 - 08:26 PM

//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.
Visit my site
Visit my github

### #2Labouts  Members   -  Reputation: 128

Like
1Likes
Like

Posted 14 January 2012 - 08:43 PM

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.

### #3RulerOfNothing  Members   -  Reputation: 961

Like
0Likes
Like

Posted 14 January 2012 - 08:57 PM

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

### #4D.Chhetri  Members   -  Reputation: 180

Like
0Likes
Like

Posted 14 January 2012 - 09:03 PM

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.
Visit my site
Visit my github

### #5boogyman19946  Members   -  Reputation: 622

Like
1Likes
Like

Posted 14 January 2012 - 10:45 PM

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.
"If highly skilled generalists are rare, though, then highly skilled innovators are priceless." - ApochPiQ

- Java API Documentation - For all your Java info needs
- C++ Standard Library Reference - For some of your C++ needs ^.^

### #6Josh Petrie  Moderators   -  Reputation: 2494

Like
1Likes
Like

Posted 15 January 2012 - 12:37 AM

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.

Josh Petrie | Core Tools Engineer, 343i | Microsoft C++ MVP

### #7D.Chhetri  Members   -  Reputation: 180

Like
0Likes
Like

Posted 15 January 2012 - 12:54 AM

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.