Search code examples
c++trie

How to define class for Trie in cpp


Hi I am new to oops in cpp. I define a class for trie Node as follows. But I get this error and I couldnot find where I am wrong. Thanks in advance.

Error : Line 5: expected identifier before numeric constant

class Node {
        public:
            bool end;
            char val;
            vector<Node *> children(26);
        Node(char val)
        {
            val=val;
            end=false;
            for(i=0;i<children.size();i++)
                children[i]=NULL;
        }
};

Solution

  • Should be

         vector<Node *> children;
    Node(char val) : end(false), val('\0'), children(26)
    {
        // ...
    

    Use the member initializer list to call specific member variable constructors.