How to use Node as a return type in a class?

Hello,

I am new to C++ and I am confused as to why I cannot utilize Node* as a return type for functions in my BST class (I have to use it as a return type).

My guess is because Node class is not declared except for in 'private'?

Though I am not sure how to fix this because I HAVE to use Node* in the rest of my BST class.

Please let me know what I could do :)

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
  P#ifndef BST_H
#define BST_H

#include <string>

using namespace std;

//DO I HAVE TO CAPITALIZE ALL 'CONSTANTS' PER THE PROG. ASSIGNMENT EXPECTATIONS?
//DO CLASS OBJECTS HAVE TO BE CAPITALIZED?

class BST
{
    public:
        BST();
        ~BST();
    
        void deleteSubtree(Node* curr_root);

        void insertContent(const string& word, const string& definition);
        void deleteContent(string* word);
        const string* getContent(const string& word);
        Node* theRoot();
    //WHY DOES COMPLILING SAY NODE HAS NOT BEEN DECLARED?
        Node* treeMinimum(Node* ptr);
        void nodeTransplant(Node* oldN, Node* newN);
    
    private:
        class Node
        {
            public:
                Node(Node* cParent, string* word, string* definition)
                {parent=cParent; left=NULL; right=NULL; m_word=word; m_definition=definition;}
            
                Node* parent;  //IS IT OKAY THAT I ADDED IN A PARENT ATTRIBUTE TO NODES?
                Node* left;
                Node* right;
                string* m_word;
                string* m_definition;
        };
    
        Node* root;
    
};



Also, I would like to know how to do the same thing almost, in another class (lines with the Node* in them return errors saying Node has not been declared)

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
    #ifndef DICTIONARY_H
    #define DICTIONARY_H
    
    #include <string>
    #include "BST.h"
    
    using namespace std;
    
    class Dictionary
    {
        public:
            Dictionary();
            ~Dictionary();
        
            void add(const string& word, const string& definition);
            void remove(const string&);
            const string* getDefinition(const string& word);
            Node* getRoot();
            void printEntry(Node* ptr);
            void printInOrder(Node* ptr);
            void printPreOrder(Node* ptr);
            void printPostOrder(Node* ptr);
        
        private:
            BST dictionary;
        
    };
    
    
    
    #endif 
Last edited on
Class Node is
a) Inner class so it can only be accessed from outside the encompassing class as Encompassing::Inner
b) private, so it cannot be accessed from outside the encompassing class at all.

In your case you might fix it by making Node class declaration public and fully qualifying its name in your Dictionary class or by making it stand alone class.
Last edited on
Topic archived. No new replies allowed.