简体   繁体   中英

use of a deleted function std::unique_ptr

The error seems to be in the functions insert and printTree. I know that this error is caused by unique_ptr being not copy-able. But I thought that providing move and copy semantics should help me solve it. My Questions

  1. Have I made the copy and move constructors correct ?

  2. If NO. How should I redesign the code. Please list out fundamental mistakes and how to correct them.

  3. How can I incorporate Node* parent in the class?

  4. Few tips on good code practices in such cases would be helpful

     // This is implementation of binary search tree. #ifndef BinarySearchTree_H #define BinarySearchTree_H #include <cstdio> #include <functional> #include <utility> #include <vector> #include <iostream> #include <memory> //template declaration template <class ValueType> class BinarySearchTree { struct Node { ValueType value; std::unique_ptr<Node> left; std::unique_ptr<Node> right; //Node *parent=nullptr; // How can I use parent in the class ? Node(){} //Node(const ValueType& value,std::unique_ptr<Node> left,std::unique_ptr<Node> right):value(value),left(left),right(right){} Node (const ValueType& value):value(value),left(nullptr),right(nullptr){} }; std::unique_ptr<Node> root; void insert(const ValueType& value, std::unique_ptr<Node> node) { if(value< node->value) { if(node->left) { insert(value,node->left); } else { std::unique_ptr<Node> left=std::unique_ptr<Node>(new Node(value)); node->left=left; } } else { if(node->right) { insert(value,node->right); } else { std::unique_ptr<Node> right=std::unique_ptr<Node>(new Node(value)); node->right=right; //right->parent=node; } } } void printNode(std::unique_ptr<Node> node) { if(!node) { std::cout<<"No element in the tree\\n"; } else { if(node->left) { std::cout<<node->left->value<<" "; } std::cout<<node->value<<" "; if(node->right) { std::cout<<node->right->value<<" "; } } } public: BinarySearchTree():root(nullptr){} ~BinarySearchTree(){} BinarySearchTree( BinarySearchTree && rhs):root(std::move(rhs.root)){} BinarySearchTree& operator=(BinarySearchTree && rhs ) { root=std::move(rhs.root); return *this; } BinarySearchTree& operator=(const BinarySearchTree & rhs ) { if(this!=&rhs) root.reset(rhs.root); return *this; } void insert(const ValueType& value) { if(root==nullptr) { root=std::unique_ptr<Node>(new Node(value)); } else { insert(value,root); } } // void remove(const ValueTypr& value); void printTree(const BinarySearchTree& tree) { if(tree.root) { if(tree.root->left) { printNode(tree.root->left); } printNode(tree.root); if(tree.root->right) { printNode(tree.root->right); } } else { std::cout<<"tree is empty\\n"; return; } } }; #endif // BinarySearchTree 
  1. No. You cannot copy unique pointers. You have to decide whether deep copies of your tree are meaningful or not.
  2. Use move constructor and move assignment operator instead of copy constructor and copy assignment operator.
  3. Add a raw pointer Node* parent . A parent owns its children, not vice-versa.
  4. Use std::make_unique() . Avoid including headers you don't need. Is there a reason why printTree() doesn't work on this ? Generally, you could use slightly a more readable syntax (indentation, empty lines etc.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM