Question

I've created a binary search tree, each node of my binary tree is setup in a struct containing the key, and a pointer to the left and right nodes.

In my copy constructor for this binary search tree, I call a helper method to recurs through the tree that looks like so:

Node* BinaryTree::copyHelper(const Node* other)
{
if(other == NULL)
{
    return NULL; // If there's no Node to copy, return NULL.
}

Node* newNode  = new Node; 

if(newNode)
{
    newNode->name  = other->name;
    newNode->left  = copyHelper(other->left); 
    newNode->right = copyHelper(other->right);
}

return newNode; 
}

My error mentioned in the title is on the left and right pointers in the final if statement above.

If someone could tell me how to remove it, that would be appreciated.

Était-ce utile?

La solution

You can probably bypass the warning if you use smart pointers instead of raw pointers:

typedef std::unique_ptr<Node> NodePtr; 
NodePtr newNode(new Node);

instead of

Node* newNode = newNode;
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top