如何使用指针数组实现节点?

时间:2015-11-09 21:40:25

标签: c++ linked-list nodes

因此,对于我的项目,我需要创建一个包含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'”。指针总是搞砸了我,我确信这是问题所在。我也不确定我是否有正确的函数实现逻辑。有语法错误吗?或者我执行错误了吗?我该如何解决这个问题?

1 个答案:

答案 0 :(得分:2)

就像这样:

Node* Node::getAttachedNode(int direction) {
    return attachedNodes[direction];
}

与其他方法一样。