문제

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.

도움이 되었습니까?

해결책

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;
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top