For educational purposes I'm writing a templated Stack class based on a singly linked list. I have written a class for a node:
template <typename T>
class StackNode {
private:
T data;
StackNode<T> *next;
// TODO: make Stack a friend
};
and I started writing the stack itself:
template <typename T>
class Stack {
private:
StackNode<T> *head;
unsigned long long int size;
public:
Stack(): head(nullptr), size(0) {}
void push(const T& element) {
StackNode<T> *new_element = new StackNode<T>;
new_element -> data = element;
new_element -> next = head;
head = new_element;
++size;
}
};
Obviously, these two lines
new_element -> data = element;
new_element -> next = head;
are giving me trouble, because I'm trying to get access to private members. I want to make Stack
a friend of StackNode
. How can I do that?
When I write friend class Stack<T>
in my TODO line in the StackNode
, my CLion IDE underlines Stack
with a red squiggly line and it says that I have redefinition of Stack
as a different kind of symbol. I don't know how to fix that.
UPD:
my main
function looks like this:
int main() {
Stack<long long int> s;
s.push(12);
return 0;
}
I appreciate any help.