Hi everyone,

So I tried making my program here, and I got an error that I can't figure out. I have a class for a Binary Tree, "Stree", with a few methods defined publicly, and a "Node" class defined privately, within "Stree". These methods use "Nodes" as parameters. Now when my main cpp file calls in stree.h, on each line that contains a method, it says "Node has not been declared". The errors are below:

g++ -Wall -g -c train.cpp
In file included from train.cpp:8:
stree.h:22: error: 'Node' has not been declared
stree.h:22: error: 'Node' has not been declared
stree.h:23: error: 'Node' has not been declared
stree.h:23: error: 'Node' has not been declared
stree.h:25: error: 'Node' has not been declared
stree.h:25: error: 'Node' has not been declared
stree.h:26: error: 'Node' has not been declared
stree.h:27: error: 'Node' has not been declared

Here is the code for stree.h:

#ifndef STREE_H
#define STREE_H

#include <string>
using namespace std;

class Stree
{
    public:
        Stree();
        ~Stree();
        void remove(string);
        void insert(string, string);
        void print(string);
        bool isEmpty();
        void distance(string, string);
    
        void searchInsert(string, string, bool, Node*, Node*);
        void searchPrint(string, Node*, bool, Node*);
        //void searchRemove(string, Node*, Node*);
        void searchStart(string, Node*, Node*);
        void searchDistance(string, int, int, Node*);
        void removeSubs(Node*);
    private:
        class Node
        {
            public:
		Node();
                Node(string);
	        string city;
                Node *m_left;
	        Node *m_right;
        };
        Node *m_head;
};

#endif

Could somebody tell me why it doesn't see "Node" as declared, even when it's in the same class as the methods?
Thanks!

It's because you are declaring the class after you are trying to use it. As a result, the compiler doesn't know anything about the Node class yet. Rearrange your class' declaration so that Node is declared before you use it.

It's just like anything else, you can't use it until you tell the compiler what it is.

It's because you are declaring the class after you are trying to use it. As a result, the compiler doesn't know anything about the Node class yet. Rearrange your class' declaration so that Node is declared before you use it.

It's just like anything else, you can't use it until you tell the compiler what it is.

Well that fixed it. Thanks! :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.