Use of undeclared identifier with Scope Resolution Operators and Header File

Hello, I've been trying to get this Trie program to work for a while, and keep getting the same error "Use of undeclared identifier" for the four functions in my class. I think the header file is set up correctly, and I've used scope resolution operators for each function in the .cpp file. I cannot figure out what else is causing this problem. All help is appreciated!

#include <iostream>
#include <string>
#include "TrieNode.h"
using namespace std;

int main()
{
string keys[] = {"sunny", "raining", "happy", "sad"};
int n = sizeof(keys)/sizeof(keys[0]);

TrieNode *root = getNode();

// Construct trie
for (int i = 0; i < n; i++)
insertNode(root, keys[i]);

// Search for different keys
searchTrie(root, "the")? cout << "Yes\n" :
cout << "No\n";
searchTrie(root, "these")? cout << "Yes\n" :
cout << "No\n";
}

#include<string>
using namespace std;

const int ALPHABET_SIZE = 26;
const int CASE = 'a';

class TrieNode
{
public:
TrieNode* children[ALPHABET_SIZE];
bool isEndOfWord; //true if node represents end of word.

TrieNode();
TrieNode *getNode();
void insertNode(TrieNode *root, string key);
bool searchTrie(TrieNode *root, string key);
};

#include <iostream>
#include <string>
#include "TrieNode.h"
using namespace std;


TrieNode::TrieNode()
{
getNode();
}

TrieNode* TrieNode::getNode()
{
TrieNode *nodePointer = NULL;
nodePointer->isEndOfWord = false;

for (int i = 0; i < ALPHABET_SIZE; i++)
nodePointer->children[i] = NULL;

return nodePointer;
}

void TrieNode::insertNode(TrieNode *root, string word)
{
TrieNode *searchPointer = root; //points to the trie we are working with

for (int i = 0; i < word.length(); i++)
{
int index = word[i] - CASE;
if (!searchPointer->children[index])
searchPointer->children[index] = getNode();

searchPointer = searchPointer->children[index];
}

searchPointer->isEndOfWord = true;
}

bool TrieNode::searchTrie(TrieNode *root, string word)
{
TrieNode *searchPointer = root;
for (int i = 0; i < word.length(); i++)
{
int index = word[i] - CASE;
if (!searchPointer->children[index])
return false;

searchPointer = searchPointer->children[index];
}

return (searchPointer != NULL && searchPointer->isEndOfWord);
}

In the main function you need to call getNode() on a TrieNode object.

E.g.
1
2
TrieNode someTrieNodeObject;
TrieNode *root = someTrieNodeObject.getNode();


I'm not sure what the getNode() function is supposed to do but it's not working. You declare a null pointer and then you try to use it to access stuff but that won't work because null means the pointer doesn't point to anything.
Last edited on
Topic archived. No new replies allowed.