c++定义一个Node类,它有一个私有变量Node(一个用于*的Node)
c++ defining a Node class, which has a private variable of Node (a Node for use in a*)
我是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
我可能错过了一些愚蠢的明显,但我该如何去完成这个?
成员必须是完整类型(正如错误消息遗忘已经告诉您的那样)。这意味着你不能有前向声明的成员。
注意,您已经可以在节点类中使用node
(作为完整类型)。
然而,定义node
类型的成员仍然是不可能的,因为这会导致无限递归。因此,如果你有一个树状模型,你可能需要一个node*
。还要注意,class Foo* member;
确实是可能的。如果你真的不能避免前向声明,这是通常的解决方案。
不能在类中声明类的对象。
可能的重复:为什么不能在同一个类中声明一个类的对象?
相反,您可以像其他人提到的那样定义指针:
node* parent
class node parent;
node parent;
或者说:
node* parent;
相关文章:
- 如何创建一个CMake变量,除非显式重写,否则使用默认值
- 删除一个线程上有数百万个字符串的大型哈希映射会影响另一个线程的性能
- 为什么两个不同的未命名名称空间可以共存于一个cpp文件中
- 为什么C++对链表中的下一个节点使用指针,而像 C# 或 Java 这样的语言只使用类 Node 的名称?
- 给一个图 G 找到此图中的所有节点 blob,以便 node.color 属性 = 特定颜色
- 用输出参数包装一个c++函数,以便在javascript/node中使用
- 如何使用 Node.js 开发一个侦听器来接收许多车辆跟踪数据(通过 TCP)
- 可以在Go中编写一个本地Node.js扩展,而不是C++吗
- 我应该包装一个完全包含在node.js函数中的C++对象吗
- Node类中公共下一个指针的分支
- 我如何使用std::string在一个c++插件的Node.js
- 应该如何绑定.用OpenNI编写一个Node.js插件
- 当我访问下一个Node指针时,c++崩溃了
- 如何从一个Node.js c++插件传递一个关联数组到js代码
- 在Node.js和v8中调用一个使用包装对象作为参数的函数
- 终端服务器上的IPC,带有一个nw.js/node.js/node-namemodule(C++)
- OOP:设计一个树,在Node类和tree类之间划分功能
- Node.js C++将多个CC文件扩展为一个模块
- 我改变了我的列表(和代码),所以它现在处理Node*而不是Node Objects,这导致了一个未定义的引用错误
- c++定义一个Node类,它有一个私有变量Node(一个用于*的Node)