One of the template classes exercises in my C++ class asks to make those two classes work:
file node.h
#ifndef node_h
#define node_h
template<typename T>
class Node
{
private:
friend class Stack;
Node(T value, Node *next);
T value;
Node *next;
};
#endif /* node_h */
and file stack.h
#ifndef stack_h
#define stack_h
#include "node.h"
template<typename T>
class Stack // ERROR HERE!
{
public:
Stack() : top(0) {}
void push(T value);
T pop();
private:
Node<T> *top;
};
template<typename T>
void Stack<T>::push(T value)
{
top = new Node<T>(value, top);
}
template<typename T>
T Stack<T>::pop()
{
T result = top->value;
top = top->next;
return result;
}
#endif /* stack_h */
I'm getting a "Redefinition of 'Stack' as different kind of symbol" thrown by the compiler. I know it has to do with the declaration of Stack as a friend class in the node.h file, but if I remove that line, then Stack does not get access to the node's private members. Why is this issue occurring?