我是 C++ 的新手,我正在尝试定义一个 Node 类,它包含有关另一个节点的信息,在这种情况下,该节点将是父节点,因此我可以使用 A* 搜索来追踪最佳路线。
到目前为止我已经尝试过(node.h 文件):
class node{
private:
int xCoord;
int yCoord;
int value;
int hueristicCost;
int pathCost;
class node parent;
public:
node(int xC, int yC, int value);
int getXPos();
int getYPos();
int getValue();
};
但这会引发编译错误:
node.h:10:13: error: field ‘parent’ has incomplete type
我可能遗漏了一些非常明显的东西,但我该如何完成呢?
最佳答案
一个成员必须是一个完整的类型(正如错误消息 oblivious 已经告诉你的那样)。这意味着您不能拥有前向声明的成员。
请注意,您已经可以在节点类中使用 node
(作为完整类型)。
但是,定义一个node
类型的成员仍然是不可能的,因为这会导致无限递归。因此,如果您有一个树状模型,您可能需要一个 node*
。还要注意 class Foo* member;
确实是可能的。如果您真的无法避免前向声明,这是通常的解决方案。
关于c++定义一个Node类,它有一个私有(private)变量Node(一个Node for use in a*),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22306380/