问题描述
我已经创建了一个二叉搜索树,我的二叉树的每个节点都是在包含键的结构中设置的,还有一个指向左右节点的指针。
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.
在我的这个二叉搜索树的拷贝构造函数中,我调用一个helper方法来遍历这样的树:
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;
}
标题中提到的我的错误是在最后的左右指针if语句如上。
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);
而不是
Node* newNode = newNode;
这篇关于Parasoft错误:应该使用用于管理资源的对象“x”指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!