jscherman jscherman - 2 months ago 17
C++ Question

Object does not name a type - C++

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 to the root 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_;

// This function is the one with errors.
Node & fatherOfNode(const Node & root, const T & key, const bool hook) const;

};

...

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 because they were implementing a function before the definition. 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?

Ben Ben
Answer

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.