vendredi 1 décembre 2017

unique_ptr of a template struct initialization

I have a binary search tree template class, with a struct Node. I am trying to use unique_ptrs of type Node in my tree, but the I cannot get it to initialize

This is my struct

template <class T>
struct Node{
    T data;
    unique_ptr<Node<T>> left;
    unique_ptr<Node<T>>  right;
    unique_ptr<Node<T>> parent;
    Node();
    Node(const T & 
    value):data(value),left(nullptr),right(nullptr),parent(nullptr){}
};

In my binary search tree class definition I have

private:
unique_ptr<Node<T>> root;

And then in a function in the bst I have

Node<T>* temp(new Node<T>(data)); //works
root(new Node<T>(data)); //does not compile

The error message printed out:

 error: no match for call to ‘(std::unique_ptr<Node<std::__cxx11::basic_string<char> >, std::default_delete<Node<std::__cxx11::basic_string<char> > > >) (Node<std::__cxx11::basic_string<char> >*)’
     root(new Node<T>(data));
     ^

Why is this happening? Any explainations/tips on using unique_ptr would be greatly appreciated.

Note: I have include and using namespace std at the top of my header file

Aucun commentaire:

Enregistrer un commentaire