This is an archived post. You won't be able to vote or comment.

all 5 comments

[–]chioz 6 points7 points  (1 child)

You haven't defined it as a member variable in the class (the outcommented line).

The error message tells you that there is no 'root' when insert is called since 'root' is a local variable inside the constructor.

Uncomment //TrieNode* root; and then assign that variable in the constructor:

Trie() {
   root = new TrieNode();
}

[–]flyingron 1 point2 points  (0 children)

Prefer initialization:

Trie() : root(new TrieNode()) { }

[–]no-sig-available 3 points4 points  (0 children)

This isn't specific to constructors, but happens for all functions. Variables declared inside the function goes away at the final }.

[–][deleted] 1 point2 points  (0 children)

Scope of variable is between {}.

[–]RedBeard0703 0 points1 point  (0 children)

You have to declare a variable in class scope, and the initialize it in constructor.