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

C++ Compiler Error; Namespace issue I guess

本文关键字:命名空间 问题 编译器 错误 C++      更新时间:2023-10-16

我得到的错误是"在名称空间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);
 }
}

你们知道吗?

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

//件.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

//板.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

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

请注意,这是跨多个文件的,所以可能是链接的问题?

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

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

原始帖子的问题是有一个循环引用。不知怎么的,这引起了麻烦。我很想解释一下。