jeudi 15 septembre 2022

How to initialize the array-like member variable in the constructor?

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);
}

Aucun commentaire:

Enregistrer un commentaire