ошибка: использование удаленной функции?

В настоящее время я пытаюсь реализовать простое 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

Я не уверен, что понимаю, почему это вызывает проблемы … Так что некоторые объяснения были бы оценены, есть ли у структур (и классов) конструктор по умолчанию? так почему он жалуется?

0

Решение

Конструктор по умолчанию node удаляется, потому что element не имеет (это, кстати, именно то, что пытается сообщить вам сообщение об ошибке;). Нет созданного компилятором конструктора по умолчанию для element потому что вы предоставили свой собственный конструктор (который не является конструктором по умолчанию, т.е. вы не можете вызывать его без аргументов). Либо напишите конструктор по умолчанию для node который передает параметры в element конструктор:

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

или один для element:

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

В последнем случае конструктор по умолчанию для node может и будет сгенерирован компилятором, потому что это действительно так:

node() {}
1

Другие решения

Других решений пока нет …

По вопросам рекламы [email protected]