How to initialize the array-like member variable?
The visual studio code says:
no matching function for call to 'Node::Node()' gcc line 12 col 9
const int N = 100;
struct Node {
int val, ch[2];
/**
void init(int _val) {
this->val = _val, this->ch[0] = this->ch[1] = 0;
}*/
Node (int _val): val(_val) {
this->ch[0] = this->ch[1] = 0;
}
} tree[N]; // <--------- this is line 12
int main() {
int a;
cin >> a;
tree[0] = Node(a);
}
tree[N], notch[2]. Like the error says, you need to give your class a default constructor. If you don't want to do that, you need to specify an initializer for each array element.std::vectorwithpush_back/emplace_backinstead of an array, which doesn't require you to immediately (default-)construct all elements.