开发者

Double Pointer Question

开发者 https://www.devze.com 2023-02-14 11:48 出处:网络
I am trying to make a tree template that can have any number of children on each node. This is my code for an addChild function in the node class -

I am trying to make a tree template that can have any number of children on each node. This is my code for an addChild function in the node class -

template<typename T>
void Tree<T>::Node::addChild(T& value) {
    Node* temp = new Node(value, this); //second parameter is for parent
    numOfChildren++;
    children*[numOfChildren] = temp;
}

Instead of having a pointer for a left and right child, I thought I should make a double pointer (pointer to an array of Node*).

Node** children;

I keep getting an "Expected primary开发者_高级运维 expression before '[' token" error. I'm guess I am accessing the 2D array wrong then? Or maybe I should just go about it a different way? Do you think it would work if I just had children as

Node* children ?

I feel like it might work if I just have a Node* and each element be a different Node.

Any help is appreciated.


Just use children[numOfChildren]. Or do it the right way with std::vector<Node *> children;

0

精彩评论

暂无评论...
验证码 换一张
取 消