我正在尝试实施BST unique_ptr.我有一份工作计划shared_ptr.我如何使用unique_ptr来强制实施BinarySearchTree的单一所有权语义?
当我替换shared_ptr时unique_ptr,我得到的编译错误超出了我的理解范围.
#include <iostream>
#include <memory>
template<class T>
class BinarySearchTree{
struct TreeNode;
typedef std::shared_ptr<TreeNode> spTreeNode;
struct TreeNode{
T data;
spTreeNode left;
spTreeNode right;
TreeNode(const T & value):data(value),left(nullptr),right(nullptr){}
};
spTreeNode root;
bool insert(spTreeNode node);
void print(const spTreeNode) const ;
public:
BinarySearchTree();
void insert( const T & node);
void print()const;
};
template<class T>
BinarySearchTree<T>::BinarySearchTree():root(nullptr){}
template<class T>
void BinarySearchTree<T>::insert(const T & ref)
{
TreeNode *node = new TreeNode(ref);
if (root==nullptr)
{
root.reset(node);
}
else
{ …Run Code Online (Sandbox Code Playgroud)