C ++编译器错误;我想是命名空间问题

时间:2012-01-15 00:59:06

标签: c++ namespaces compiler-errors

我得到的错误是“名称空间ChessGame中没有成员命名的详细信息。这是相关代码

//ChessPiece.h
namespace ChessGame 
{

class ChessBoard;

namespace detail
{
    class IChessPieceEnums{
    public:
        enum PieceType{PAWN, KNIGHT, BISHOP, ROOK, QUEEN, KING};
        enum PieceDirection{ UP = 1 , DOWN  = -1};
        enum PieceId{ WHITE_PIECE_ID, BLACK_PIECE_ID };
    };
}

//represents an abstract chess piece interface
class IChessPiece : public detail::IChessPieceEnums
{
public:   
 ///...
}

} // end namespace


//GameBoard.h
#include "ChessPiece.h"
namespace ChessGame 
{

 class IChessPiece;

 class ChessBoard
 {
  public:
    /*********ERROR OCCURS ON THIS FUNCTION************/
    bool isOccupiedWithEnemy(int row, int col,const ChessGame::detail::IChessPieceEnums::PieceId& pieceId);
 }
}

任何想法的人?

编辑:另一个最小的例子:

// 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{
    bool isOcc(int x, int y,const detail::PieceEnums::PieceID pid)const;
};
}

#endif

错误是'使用未声明的标识符详细信息

请注意,这是跨多个文件,因此链接可能存在问题吗?

2 个答案:

答案 0 :(得分:1)

要直接指定所需的名称,请说出detail::IChessPieceEnums::PieceId::ChessGame::detail::IChessPieceEnums::PieceId,但最好是前者。但是,您目前的语法实际上也很好,因为如果找不到名称,搜索将在全局命名空间中恢复。

答案 1 :(得分:0)

好的找到了解决方案。解决方案是将名称空间详细信息放在名为detail.h的文件中。这样,piece.h和board.h需要包含details.h才能使用它。这很有用。

原帖的问题在于有一个循环引用。这在某种程度上造成了麻烦。会喜欢解释。