【发布时间】:2012-05-02 14:19:22
【问题描述】:
在二叉搜索树的类中,我有一个键和一个指向结构中定义的左右节点的指针。
我在类的复制辅助函数中遇到 parasoft 错误,因此建议将代码更改为:
BinaryTree::Node* BinaryTree::copyHelper(const Node* other)
{
if(other == NULL)
{
return NULL; // If there's no Node to copy, return NULL.
}
else
{
//Node* newNode = new Node;
typedef std::unique_ptr<Node> NodePtr;
NodePtr newNode(new Node);
if(newNode)
{
newNode->name = other->name;
newNode->left = copyHelper(other->left);
newNode->right = copyHelper(other->right);
}
return newNode;
}
}
现在我在 newNode 的 return 语句上遇到错误:
IntelliSense:没有合适的从
NodePtr到BinaryTree::Node *的转换函数
有什么想法吗?
【问题讨论】:
-
您能否检查一下您的代码示例,目前您对
newNode的定义无法编译。 -
unique_ptr 的 ctor 是显式的
-
您要返回什么,
unique_ptr<Node>或Node*?如果你想要 Node*,那么它不需要在你的函数中有 NodePtr。 -
我以前用过Node* newNode = new Node;但被告知要使用新的代码stackoverflow.com/questions/10414869/… 我仍然想返回我之前返回的内容,但使用智能指针代替。
-
您似乎不明白为什么需要智能指针并尝试这样做是因为互联网上的某人告诉了您。他们告诉你的恰好是很好的建议,但如果你了解为什么这是很好的建议,你可能就不需要问这个问题了。
标签: c++ c++11 smart-pointers