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?
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.
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++).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With