如何使用指针数组实现节点
How to implement a node with an array of pointers?
因此,对于我的项目,我需要制作一个包含指向4个不同方向的节点的链表。这是节点声明:
class Node {
public:
Node(string newname);
Node();
void setNodeName(string newname);
string getNodeName();
void attachNewNode(Node *newNode, int direction);
Node *getAttachedNode(int direction);
private:
string name;
Node *attachedNodes[4];
};
这是我的实现:
Node::Node(string newname) {
newname = name;
for (int i = 0; i < 3; i++) {
attachedNodes[i] = NULL;
}
}
Node::Node() {
for (int i = 0; i < 3; i++) {
attachedNodes[i] = NULL;
}
}
void Node::setNodeName(string newname) {
newname = name;
}
string Node::getNodeName() {
return name;
}
void Node::attachNewNode(Node *newNode, int direction){
newNode = attachedNodes[direction];
}
Node *getAttachedNode(int direction) {
return attachedNodes[direction];
}
代码getAttachedNode(int direction)方法在返回行中给出错误:"使用未声明的标识符'attachedNodes'"。指针总是把我搞砸,我相信这就是问题所在。我也不确定我是否有用于函数实现的正确逻辑。有语法错误吗?还是我实施它们错了?我该如何解决这个问题?
就像这样:
Node* Node::getAttachedNode(int direction) {
return attachedNodes[direction];
}
就像你的其他方法一样。
相关文章:
- 为什么我们要为avl树实现返回一个指向节点的指针,而不是void函数
- 如何在C++中实现节点数任意的通用树数据结构?
- C++ 如何使用动态计算的新节点实现 A*?
- 语言分析的二叉树实现 - 子节点:不起作用
- 为什么T是未定义的?我正在尝试实现一个用于双链表的节点类,它不喜欢我使用友元运算符后的T
- 在不创建新节点的情况下实现带有映射的trie
- 如何使用 std::list 作为公开节点的列表的接口的实现?
- 如何在 c++ 中实现 avl 树,每个节点都是另一个 avl 树
- C++ 链表合并排序的实现在连接 1 个以上节点的子列表时失败
- 如何实现链表并允许用户选择要使用 C++ 删除的特定节点?
- 在C++中实现二叉搜索树,搜索不起作用.尝试打印节点的元素会导致输出崩溃
- 如何在 C++ 与 CPLEX 中实现宏节点回调
- 双向链表插入方法实现 - 正在搜索哪个节点
- 如何在C++中实现按级别计数/显示节点的功能
- 在我的C++链表实现中取消引用节点指针,给出意想不到的结果
- 使用深度第一次搜索的无向图中的图形调查无法实现的节点
- 如何在节点类中实现节点引用
- 图C++的实现(节点是字符)
- 如何使用指针数组实现节点
- 实现节点插件在Xcode