Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Object does not name a type - C++

Tags:

I'm trying to implement a Set based on a Binary Tree Search. So i'm building this set starting from a root (pointer to Node), where Node has a value, right and left child (both pointers to Node also). So this way i could set a new node on the right of the root node by pointing root->right to the created node and so on. Take a look at the definitions:

template <class T>
class Set
{
    public:
        Set();
        ~Set();
        void push(const T&);
        bool belongs(const T&) const;
        void remove(const T&);
        const T& min() const;
        const T& max() const;
        unsigned int cardinal() const;
        void show(std::ostream&) const;

        friend ostream& operator<<(ostream& os, const Set<T> &c) {
            c.show(os);
            return os;
        }

    private:

        struct Node
        {
            Node(const T& v);
            T value;
            Node* left;
            Node* right; 
        };

        Node* root_;
        int cardinal_;

    Node & fatherOfNode(const Node & root, const T & key, const bool hook) const;

};

...

// This function is the one with errors.
template <class T>
Node & Set<T>::fatherOfNode(const Node & root, const T & key, const bool hook) const {
    // Some code
}

So i'm having this error:

/home/jscherman/ClionProjects/algo2-t3-bts/set.hpp:247:1: error: ‘Node’ does not name a type
 Node & Set<T>::fatherOfNode(const Node & root, const T & key, const bool hook) const {
 ^

I've seen a lot of posts related with this error, but most of them were caused by writing functions implementations before its definitions. As you can see, the implementation of fatherOfNode is below its definition, so it doesn't seem to be my case.

Any idea about what is going on?

like image 355
jscherman Avatar asked Sep 29 '16 23:09

jscherman


People also ask

Does not name a type C?

The "error does not name a type" in C/C++ is defined as the when user declares outside of the function or does not include it properly in the main file this error will through.

How do you fix a undefined reference in C++?

When we compile these files separately, the first file gives “undefined reference” for the print function, while the second file gives “undefined reference” for the main function. The way to resolve this error is to compile both the files simultaneously (For example, by using g++).


1 Answers

Node is an internal class in Set, so outside of this class you need to resolve this with:

Set<T>::Node 

So your function definition will need to be:

template <class T>
typename Set<T>::Node & Set<T>::fatherOfNode(const Set<T>::Node & root, const T & key, const bool hook) const {

Here it is, working.

like image 137
Fantastic Mr Fox Avatar answered Oct 10 '22 23:10

Fantastic Mr Fox