[英]access to an object member passed by unique_ptr
我编写了以下代码来检查节点是否在 BST 中:
bool BST_Node :: BST_Find(unique_ptr<BST_Node> root, int key){
if(!root || root->key == INT_MIN) return false;
if(root->key == key) return true;
else if(key < root->key) BST_Find(move(root->left), key);
else BST_Find(move(root->right), key);
}
root参数使用move(bst)
传递,其中bst在 unique_ptr 中。 问题是当它尝试读取root->key
时:即使键存在于树中,此方法也会返回 false。 我尝试使用调试器,但无法访问root 。
以下是使用此方法的代码:
auto bst = make_unique<BST_Node>();
for(int i=0; i<n; i++){
key = rand();
if(!bst->BST_Find(move(bst), key)) {
bst->BST_Insert(move(bst), key, "");
}
}
尝试这个
bool BST_Node :: BST_Find(unique_ptr<BST_Node> const &root, int key){
if(!root || root->key == INT_MIN) return false;
if(root->key == key) return true;
else if(key < root->key) return BST_Find(root->left, key);
else return BST_Find(root->right, key);
}
BST_Find(bst, 42) // no move
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.