c++ - 错误 : use of deleted function?

标签 c++ tree

我目前正在尝试实现一个简单的 btree,但似乎在这里遇到了问题。

#include <iostream>
#include <cstdlib>
#include <vector>
#include <algorithm>

struct element
{
    int x;
    int y;
    element (int x, int y)
    {
        this->x = x;
        this->y = y;
    }
};

int sum(element key)
{
    return key.x + key.y;
}

struct node
{
    element key;
    node *child_left;
    node *child_right;
};


class tree{

    void insert(element key);
    void insert(element key, node* leaf);
    node* root;
    tree()
    {
        this->root = NULL;
    }
};

void tree::insert(element key){
    if(this->root != NULL){
        insert(key, this->root);
    }else{
        this->root = new node;
        this->root->key.x = key.x;
        this->root->key.y = key.y;
        this->root->child_left = NULL;
        this->root->child_right = NULL;



    }
}


void tree::insert(element key, node* leaf){
    if(sum(key) < sum(leaf->key)){
        if(leaf->child_left != NULL){
            insert(key, leaf->child_left);
        }else{
          leaf->child_left = new node;
          leaf->child_left->key.x = key.x;
          leaf->child_left->key.y = key.y;
          leaf->child_left->child_left = NULL;
          leaf->child_left->child_right = NULL;
        }
    }else if(sum(key) >= sum(leaf->key)){
        if(leaf->child_right != NULL){
            insert(key, leaf->child_right);
        }else{
            leaf->child_right = new node;
            leaf->child_right->key.x = key.x;
            leaf->child_right->key.y = key.y;
            leaf->child_right->child_left = NULL;
            leaf->child_right->child_right = NULL;
        }
    }
}

int main()
{

    std::cout << "Somet"<< std::endl;

}

https://wandbox.org/permlink/0InxxBLt59PFVppt

这给了我错误信息:

Start
prog.cc: In member function 'void tree::insert(element)':
prog.cc:49:20: error: use of deleted function 'node::node()'
   this->root = new node;
                    ^~~~
prog.cc:26:8: note: 'node::node()' is implicitly deleted because the default definition would be ill-formed:
 struct node
        ^~~~
prog.cc:26:8: error: no matching function for call to 'element::element()'
prog.cc:14:5: note: candidate: 'element::element(int, int)'
     element (int x, int y)
     ^~~~~~~
prog.cc:14:5: note:   candidate expects 2 arguments, 0 provided
prog.cc:10:8: note: candidate: 'constexpr element::element(const element&)'
 struct element
        ^~~~~~~
prog.cc:10:8: note:   candidate expects 1 argument, 0 provided
prog.cc:10:8: note: candidate: 'constexpr element::element(element&&)'
prog.cc:10:8: note:   candidate expects 1 argument, 0 provided
prog.cc: In member function 'void tree::insert(element, node*)':
prog.cc:66:28: error: use of deleted function 'node::node()'
     leaf->child_left = new node;
                            ^~~~
prog.cc:76:28: error: use of deleted function 'node::node()'
    leaf->child_right = new node;
                            ^~~~
1
Finish

我不确定我是否理解为什么这会导致问题...所以一些解释将不胜感激,结构(和类)本身是否具有默认构造函数?那为什么要提示呢?

最佳答案

node 的默认构造函数被删除,因为 element 没有(顺便说一句,这正是错误消息试图告诉您的内容;)。没有编译器为 element 生成默认构造函数,因为您提供了自己的构造函数(这不是默认构造函数,即您不能在没有参数的情况下调用它)。要么为 node 编写默认构造函数,将参数传递给 element 构造函数:

node() : key(element(1,2)) {}

或一个用于元素:

element(int x=0,int y=0) : x(x),y(y) {}

在后一种情况下,node 的默认构造函数可以而且将会由编译器生成,因为它实际上就是这样:

node() {}

关于c++ - 错误 : use of deleted function?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47704900/

相关文章:

c++ - 如何比较程序中的两个日期?

c++ - 对 `` CLion 的 undefined reference

C++ 树类 : constructor/copy/memory leak?

java - 我应该使用八角树吗?

algorithm - 极小极大评价函数

c++ - 为什么根值没有更新

r - 在 R 中的随机森林中提取树的每个最终节点的类分布

java - 将 NewDirectByteBuffer 从 C++ 传递到 Java (JNI)

C++ 在数组或字符串中插入字符

c++ - sf::Sprite 是白色矩形(纹理试图通过引用传递)