Help in debugging simple c++ code

This is a simple trie structure .but everytime i run it i get this error
" no member named 'children' in 'Trie' ".
But clearly children is defined in Trie .I've been trying to solve it for last 5+ hours .Someone please help

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
  class Trie {
public:
    /** Initialize your data structure here. */
    Trie() {
        string val="";
        Trie* children[26];
        bool flag=false;
    }
    Trie* root=new Trie();
    /** Inserts a word into the trie. */
    void insert(string word) {
        Trie* present=root;
        for(int i=0;i<word.size();i++)
        {
            if(present->children[word[i]-'a']==NULL)
            {
                Trie* p=new Trie();
                p->val=present->val+word[i];
                present->children[word[i]-'a']=p;
                present=p;
            }
            else
                present=present->children[word[i]-'a'];
        w}
        present->flag=true;
    }
    
    /** Returns if the word is in the trie. */
    bool search(string word) {
        Trie* present=root;
        for(int i=0;i<word.size();i++)
        {
            if(present->children[word[i]-'a']==NULL)
                return false;
            else
                present=present->children[word[i]-'a'];
        }
        return true;
    }
    
    /** Returns if there is any word in the trie that starts with the given prefix. */
    bool startsWith(string prefix) {
        Trie* present=root;
        for(int i=0;i<word.size();i++)
        {
            if(present->children[word[i]-'a']==NULL)
                return false;
            else
                present=present->children[word[i]-'a'];
        }
        for(int i=0;i<26;i++)
        {
            if(present->children[i]!=NULL)
                return true;
        }
        return false;
    }
};

/**
 * Trie object will be instantiated and called as such:
 * Trie* obj = new Trie();
 * obj->insert(word);
 * bool param_2 = obj->search(word);
 * bool param_3 = obj->startsWith(prefix);
 */
> But clearly children is defined in Trie
It's a local variable inside a member function, but that doesn't make it a class variable.

1
2
3
4
5
6
7
8
9
10
11
12
  class Trie {
private:
    // Here go the member variables
    Trie* root;
    string val;
    Trie* children[26];
    bool flag;

public:
    /** Initialize your data structure here. */
    Trie() {
    }

Now complete your constructor.
Last edited on
Topic archived. No new replies allowed.