コンストラクタ/演算子の呼び出しに問題があります。私はノードへの一意のポインタであるクラスツリーを持っています。これは、移動とコピーコンストラクタ/演算子を実装する私のコードです。C++ 14がコピーコンストラクタまたはoperator =をユニークポインタから継承するクラスの呼び出しできません
template <class Key, class Data>
class Node;
template <class Key, class Data>
class Tree : public unique_ptr<Node<Key, Data>>
{
using unique_ptr<Node<Key, Data>>::unique_ptr;
public:
/*Default empty constructor*/
Tree(){
this->reset();
}
/*Default constructor*/
Tree(const Key& key, const Data& data) : unique_ptr<Node<Key, Data>>(make_unique<Node<Key, Data>>(key, data)) {
}
/*Copy constructor*/
Tree(const unique_ptr<Node<Key, Data>>& tree) : unique_ptr<Node<Key, Data>>(nullptr){
if(tree){
this->reset(new Node<Key, Data>(*tree));
}
}
/*Move constructor: roep de move constructor van unique_ptr op*/
Tree(unique_ptr<Node<Key, Data>>&& tree) : unique_ptr<Node<Key, Data >>(move(tree)) {
}
/*Copy operator*/
Tree<Key, Data>& operator=(const unique_ptr<Node<Key, Data>>& tree) {
if (this != &tree) {
this->reset(make_unique(Tree<Key, Data>(*tree)));
if ((*this)->left != nullptr) {
(*this)->left = tree->left;
}
if ((*this)->right != nullptr) {
(*this)->right = tree->right;
}
}
return *this;
}
/*Move operator*/
Tree<Key, Data>& operator=(unique_ptr<Node<Key, Data >>&& tree) {
if (this != &tree) {
*this = unique_ptr<Key, Data>::operator=(std::move(tree));
}
return *this;
}}
私は、コンストラクタや演算子を使用してbの木にツリーをコピーしようとする=私は演算子は暗黙的に削除された私に言って、エラーを取得します。 move/copyコンストラクタ/演算子を実装すると、デフォルトのものはもはや使用できなくなり、他のすべてを実装する必要があります。しかし私の視点からは、これらのすべてを実装しました。
コード例
Tree<int, char> tree;
Tree<int, char> copy;
copy = tree;
エラー:そのコピー代入演算子は、暗黙のうちに
"*クラスツリーがノード*への一意のポインタです" - 単純な 'std :: unique_ptr'を使わない理由は何ですか?あなたのクラスは** unique_ptr **から削除されたコピーコンストラクタとコピー代入演算子を継承します。それはあなたが望むものではうまくいかないでしょう。 'unique_ptr'なら' Tree'をなぜコピーしたいのですか? –
Fureeish
私の割り当てからあらかじめ書かれたクラスヘッダーに従います。現在のタスクは、移動/コピーコンストラクタ/演算子を実装することです。目標は赤黒の木のボトムアップバージョンを実装することでした。ユニークなポインタは、(サブ)ツリーのルートを示します。回転操作を使用すると、ユニークなポインタの所有者/親が変更されます。 – PsyDough