So, moving from Linked List, I now have to build a Linked Stack, which I think is pretty much similar to it. However, I get an access error, saying that cannot access to private member which I do not understand, since I was not trying to access any private members at all....
LinkNode.h
#include <iostream>
#include <memory>
using namespace std;
template <class T>
class LinkedNode
{
public:
LinkedNode(T newElement, unique_ptr<LinkedNode<T>> newNext):element(newElement), next(newNext ? new LinkedNode<T>newNext : nullptr)
{
}
T GetElement() {return element;}
void SetElement(T x) {element = x;}
unique_ptr<LinkedNode<T>> newNext() {return next;}
void SetNext(unique_ptr<LinkedNode<T>> newNext) {next = newNext;}
private:
T element;
unique_ptr<LinkedNode<T>> next;
};
CompactStack.h
#pragma once
#include"LinkedNode.h"
using namespace std;
template <class T>
class CompactStack
{
public:
CompactStack() {}
bool IsEmpty() const { return head == 0; }
T Peek()
{
assert(!IsEmpty());
return head-> GetElement();
}
void Push(T x)
{
unique_ptr<LinkedNode<T>> newhead(new LinkedNode<T>(x, head));
head.swap(newhead);
}
void Pop()
{
assert(!IsEmpty());
unique_ptr<LinkedNode<T>> oldhead = head;
head = head->next();
}
void Clear()
{
while (!IsEmpty())
Pop();
}
private:
unique_ptr<LinkedNode<T>> head;
};
This is the error that I've got from the compiler
Error 1 error C2248: 'std::unique_ptr<_Ty>::unique_ptr' : cannot access private member declared in class 'std::unique_ptr<_Ty>' e:\fall 2013\cpsc 131\hw4\hw4\hw4\compactstack.h 23