错误:使用删除的功能?

时间:2017-12-07 22:29:04

标签: 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

我不确定我理解为什么会导致问题......所以有些解释会被理解,结构(和类)本身是否有默认构造函数?那为什么抱怨?

1 个答案:

答案 0 :(得分:1)

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

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

element的一个:

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

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

node() {}