所以,从 Linked List 开始,我现在必须构建一个 Linked Stack,我认为它与它非常相似。但是,我收到一个访问错误,说无法访问我不理解的私有成员,因为我根本没有尝试访问任何私有成员......
链接节点.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;
};
这是我从编译器得到的错误
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