Including 2 classes in headers

I want to turn this single .cpp file into multiple ones but I'm having trouble doing so. I'm getting undeclared identifier and 'children' : is not a member of 'Node' errors. Thanks so much. First time on this forum!

The original entire code
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
  #include <iostream>
#include <vector>
using namespace std;

class Node {
public:
    Node();
    ~Node() {}
    char content();
    void setContent(char c);
    bool wordMarker();
    void setWordMarker();
    Node* findChild(char c);
    void appendChild(Node* child);
    vector<Node*> children();

private:
    char m_Content;
    bool m_Marker;
    vector<Node*> m_Children;
};

class Trie {
public:
    Trie();
    ~Trie();
    void addWord(string s);
    bool searchWord(string s);
    void deleteWord(string s);
private:
    Node* root;
};

Node::Node() 
{ 
    m_Content = ' '; m_Marker = false; 
}

char Node::content() 
{ 
    return m_Content; 
}
void Node::setContent(char c) 
{ 
    m_Content = c; 
}
bool Node::wordMarker() 
{ 
    return m_Marker; 
}
void Node::setWordMarker() 
{ 
    m_Marker = true; 
}
void Node::appendChild(Node* child) 
{ 
    m_Children.push_back(child); 
}
vector<Node*> Node::children() 
{ 
    return m_Children; 
}

Node* Node::findChild(char c)
{
    for (int i = 0; i < m_Children.size(); i++)
    {
        Node* tmp = m_Children.at(i);
        if (tmp->content() == c)
        {
            return tmp;
        }
    }

    return NULL;
}

Trie::Trie()
{
    root = new Node();
}

Trie::~Trie()
{
    // Free memory
}

void Trie::addWord(string s)
{
    Node* current = root;

    if (s.length() == 0)
    {
        current->setWordMarker(); // an empty word
        return;
    }

    for (int i = 0; i < s.length(); i++)
    {
        Node* child = current->findChild(s[i]);
        if (child != NULL)
        {
            current = child;
        }
        else
        {
            Node* tmp = new Node();
            tmp->setContent(s[i]);
            current->appendChild(tmp);
            current = tmp;
        }
        if (i == s.length() - 1)
            current->setWordMarker();
    }
}


bool Trie::searchWord(string s)
{
    Node* current = root;

    while (current != NULL)
    {
        for (int i = 0; i < s.length(); i++)
        {
            Node* tmp = current->findChild(s[i]);
            if (tmp == NULL)
                return false;
            current = tmp;
        }

        if (current->wordMarker())
            return true;
        else
            return false;
    }

    return false;
}

// Test program
int main()
{
    Trie* trie = new Trie();
    trie->addWord("Hello");
    trie->addWord("Balloon");
    trie->addWord("Ball");

    if (trie->searchWord("Hell"))
        cout << "Found Hell" << endl;

    if (trie->searchWord("Hello"))
        cout << "Found Hello" << endl;

    if (trie->searchWord("Helloo"))
        cout << "Found Helloo" << endl;

    if (trie->searchWord("Ball"))
        cout << "Found Ball" << endl;

    if (trie->searchWord("Balloon"))
        cout << "Found Balloon" << endl;

    delete trie;
}


My attempt:
node.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
node.h
#ifndef NODE_INCLUDED
#define NODE_INCLUDED

class Node {
public:
    Node();
    ~Node() {}
    char content();
    void setContent(char c);
    bool wordMarker();
    void setWordMarker();
    Node* findChild(char c);
    void appendChild(Node* child);
    vector<Node*> children();

private:
    char m_Content;
    bool m_Marker;
    vector<Node*> m_Children;
};

#endif // NODE_INCLUDED 


trie.h
1
2
3
4
5
6
7
8
9
10
11
12
13
#ifndef TRIE_INCLUDED
#define TRIE_INCLUDED
class Trie {
public:
    Trie();
    ~Trie();
    void addWord(string s);
    bool searchWord(string s);
    void deleteWord(string s);
private:
    Node* root;
};
#endif //TRIE_INCLUDED 


node.cpp
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
#include "node.h"
#include <iostream>
#include <vector>
using namespace std;

Node::Node() 
{ 
    m_Content = ' '; m_Marker = false; 
}

char Node::content() 
{ 
    return m_Content; 
}
void Node::setContent(char c) 
{ 
    m_Content = c; 
}
bool Node::wordMarker() 
{ 
    return m_Marker; 
}
void Node::setWordMarker() 
{ 
    m_Marker = true; 
}
void Node::appendChild(Node* child) 
{ 
    m_Children.push_back(child); 
}
vector<Node*> Node::children() 
{ 
    return m_Children; 
}

Node* Node::findChild(char c)
{
    for (int i = 0; i < m_Children.size(); i++)
    {
        Node* tmp = m_Children.at(i);
        if (tmp->content() == c)
        {
            return tmp;
        }
    }

    return NULL;
}


trie.cpp
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
Trie::Trie()
{
    root = new Node();
}

Trie::~Trie()
{
    // Free memory
}

void Trie::addWord(string s)
{
    Node* current = root;

    if (s.length() == 0)
    {
        current->setWordMarker(); // an empty word
        return;
    }

    for (int i = 0; i < s.length(); i++)
    {
        Node* child = current->findChild(s[i]);
        if (child != NULL)
        {
            current = child;
        }
        else
        {
            Node* tmp = new Node();
            tmp->setContent(s[i]);
            current->appendChild(tmp);
            current = tmp;
        }
        if (i == s.length() - 1)
            current->setWordMarker();
    }
}


bool Trie::searchWord(string s)
{
    Node* current = root;

    while (current != NULL)
    {
        for (int i = 0; i < s.length(); i++)
        {
            Node* tmp = current->findChild(s[i]);
            if (tmp == NULL)
                return false;
            current = tmp;
        }

        if (current->wordMarker())
            return true;
        else
            return false;
    }

    return false;
}


main.cpp
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
#include "node.h"
#include <iostream>
#include <vector>
using namespace std;

// Test program

#include "node.h" //is this needed?
#include "trie.h" //is this needed?
int main()
{
    Trie* trie = new Trie();
    trie->addWord("Hello");
    trie->addWord("Balloon");
    trie->addWord("Ball");

    if (trie->searchWord("Hell"))
        cout << "Found Hell" << endl;

    if (trie->searchWord("Hello"))
        cout << "Found Hello" << endl;

    if (trie->searchWord("Helloo"))
        cout << "Found Helloo" << endl;

    if (trie->searchWord("Ball"))
        cout << "Found Ball" << endl;

    if (trie->searchWord("Balloon"))
        cout << "Found Balloon" << endl;

    delete trie;
}
For a start your not including "trie.h" in trie.cpp and with the is the needed comments, I'd recommend including both node.h and trie.h once in main.cpp, placing it between the standard library includes and the using namespace std
Thanks I've fixed that. In node.h, both vector<Node*> children(); and vector<Node*> m_Children; give me the error:

Error 10 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
Try #include <vector> and having the std:: prefix before vector.
Last edited on
great will try that when I wake up. It's 4am here.

Also is it possible I cant use a class pointer inside of itself?
No, there is no problem using a Node pointer inside a Node.
Note that you use Node inside class Trie, so near the top of trie.h, you should add
class Node;
This is a forward declaration. It tells the compiler that there is a class called Node and you'll be providing a full declaration later on.

Topic archived. No new replies allowed.