C++编译器错误;我想是命名空间问题
C++ Compiler Error; Namespace issue I guess
我得到的错误是"在名称空间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才能使用它。这很有效。
原始帖子的问题是有一个循环引用。不知怎么的,这引起了麻烦。我很想解释一下。
相关文章:
- 有关命名空间和模板参数的名称的问题
- 命名空间范围问题
- 在 c++ 中定义命名空间中模板类的友元函数时出现问题
- 从命名空间嵌入资源的问题(C++和CMake)
- 与自定义命名空间一起使用时实现 std::error_category、名称解析问题
- 关于"使用命名空间标准"的问题?
- 更改命名空间以自定义 Boost XML 的标记名称后的反序列化问题
- 有关通过派生类在命名空间内继承的问题
- C++相同的命名空间问题
- C++11 中的命名空间问题?
- C++中的编译错误可能是命名空间问题
- 命名空间问题:前向声明和混合命名空间
- C++命名空间问题
- LLVM设置函数传递的问题-命名空间问题
- C++中的命名空间问题
- 如何解决此命名空间问题
- C++编译器错误;我想是命名空间问题
- c++ python构造函数类型错误(可能的命名空间问题)
- 如何避免juice框架和OpenCV之间的干扰命名空间问题
- 枚举的命名空间问题