简体   繁体   English

c++ 初始化二叉树节点

[英]c++ initializing a binary tree node

I get these errors: e0289 no instance of constructor...matches argument list and c2440: 'initializing' cannot convert from initializer list to BinaryTreeNod我收到这些错误:e0289 没有构造函数实例...匹配参数列表和 c2440:'initializing' cannot convert from initializer list to BinaryTreeNod

I am trying to point head to left and right subtree.我试图指向左右子树。


#include <memory>

using namespace std;

template <typename T>
struct BinaryTreeNode{
  T data;
  unique_ptr<BinaryTreeNode<T>> left, right;

  explicit BinaryTreeNode(const T& data) : data(data) {}
  BinaryTreeNode(T data, unique_ptr<BinaryTreeNode<T>> left,
  unique_ptr<BinaryTreeNode<T>> right) : data(data), left(move(left)),
  right(move(right)) {}
};

int main()
{

  BinaryTreeNode<int> subtree_0{ 5 };
  BinaryTreeNode<int> subtree_1{ 7 };
  BinaryTreeNode<int> head{3, subtree_0, subtree_1 };
}

the error comes from the line BinaryTreeNode head{3, subtree_0, subtree_1 };错误来自BinaryTreeNode head{3, subtree_0, subtree_1 } 行; . . Is something wrong with the constructor?构造函数有问题吗? Or am I doing smth wrong trying to initialize like that?或者我在尝试这样初始化时做错了什么? Just trying to make a simple binary tree (the code of the BinaryTree Node is from "Elements of Programming Interviews"只是尝试制作一个简单的二叉树(BinaryTree 节点的代码来自“编程面试元素”

std::unique_ptr does not support implicit pointer to unique pointer conversion, but it has an explicit constructor to do so. std::unique_ptr不支持隐式指针到唯一指针的转换,但它有一个显式构造函数可以这样做。 But assigning it a stack allocated pointer is not advised as it might try to delete it later on.但是不建议为其分配堆栈分配的指针,因为它可能稍后会尝试删除它。

To resolve this, we need to make the two nodes out of unique pointers.为了解决这个问题,我们需要用唯一的指针来制作这两个节点。

std::unique_ptr<BinaryTreeNode<int>> subtree_0 = std::make_unique<BinaryTreeNode<int>>(BinaryTreeNode<int>(5));
std::unique_ptr<BinaryTreeNode<int>> subtree_1 = std::make_unique<BinaryTreeNode<int>>(BinaryTreeNode<int>(7));

But as the spec states, you cannot copy any unique pointers.但正如规范所述,您不能复制任何唯一指针。 Which means that we need to move these two pointers to the tree node object.这意味着我们需要将这两个指针移动到树节点 object。 For this we need to accept them as rvalues in the constructor.为此,我们需要在构造函数中接受它们作为右值 Then we can move the pointers to the object without an issue.然后我们可以毫无问题地将指针移动到 object。

template <typename T>
struct BinaryTreeNode {
    ...
    BinaryTreeNode(T data, std::unique_ptr<BinaryTreeNode<T>>&& left,
        std::unique_ptr<BinaryTreeNode<T>>&& right) : data(data), left(std::move(left)), right(std::move(right)) {}
};

int main()
{
    std::unique_ptr<BinaryTreeNode<int>> subtree_0 = std::make_unique<BinaryTreeNode<int>>(BinaryTreeNode<int>(5));
    std::unique_ptr<BinaryTreeNode<int>> subtree_1 = std::make_unique<BinaryTreeNode<int>>(BinaryTreeNode<int>(7));
    BinaryTreeNode<int> head{ 3, std::move(subtree_0), std::move(subtree_1) };
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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