gpt4 book ai didi

c++ - 如何在模板类中使用唯一指针深度复制构造函数?

转载 作者:行者123 更新时间:2023-11-28 03:07:54 25 4
gpt4 key购买 nike

因此,从链表开始,我现在必须构建一个链栈,我认为它与它非常相似。但是,我收到一个访问错误,说无法访问私有(private)成员。我知道它与唯一指针内的构造函数有关,您无法在其中复制指针。其中一个人告诉我对构造函数进行深度复制,但我不知道该怎么做。谁能告诉我该怎么做?谢谢。

PS:我知道这篇文章是我今天早些时候发布的。但我自己还没有答案,似乎周围也没有人回答我,所以我决定重新发布它。如果您认为这是转发,请随时将其删除。

链接节点.h

#include <iostream>
#include <memory>
using namespace std;

template <class T>
class LinkedNode
{

public:
// This is giving me error and I do not know how to recreate
// or deep-copy the constructor
LinkedNode(T newElement, unique_ptr<LinkedNode<T>> newNext)
{
element = newElement;
next = newNext
}

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

最佳答案

unique_ptr 必须使用 move 从一个 unique_ptr 转移到另一个。下面我添加了编译所需的所有 Action 。请注意,移动后,原始对象处于有效但未知的状态。这在 Pop() 方法中很重要。

using namespace 放在 header 中也是不好的做法,因此它们已被删除。

链接节点.h

#pragma once
#include <memory>

template <class T>
class LinkedNode
{
public:
LinkedNode(T newElement, std::unique_ptr<LinkedNode<T>> newNext)
{
element = newElement;
next = move(newNext);
}
T GetElement() {return element;}
void SetElement(T x) {element = x;}
std::unique_ptr<LinkedNode<T>> newNext() {return move(next);}
void SetNext(std::unique_ptr<LinkedNode<T>> newNext) {next = move(newNext);}

private:
T element;
std::unique_ptr<LinkedNode<T>> next;
};

CompactStack.h

#pragma once
#include <cassert>
#include"LinkedNode.h"

template <class T>
class CompactStack
{
public:
CompactStack() {}
bool IsEmpty() const { return head == nullptr; }
T Peek()
{
assert(!IsEmpty());
return head-> GetElement();
}
void Push(T x)
{
std::unique_ptr<LinkedNode<T>> newhead(new LinkedNode<T>(x, move(head)));
head.swap(newhead);
}
void Pop()
{
assert(!IsEmpty());
std::unique_ptr<LinkedNode<T>> oldhead = move(head);
head = oldhead->newNext(); // head no longer valid after move, use oldhead
// oldhead->next no longer valid, but local variable going out of scope anyway.
}
void Clear()
{
while(!IsEmpty())
Pop();
}
private:
std::unique_ptr<LinkedNode<T>> head;
};

以上适用于简单测试:

#include <iostream>
#include "CompactStack.h"
using namespace std;
int main()
{
CompactStack<int> cs;
cout << "IsEmpty " << cs.IsEmpty() << endl;
cs.Push(1);
cout << "IsEmpty " << cs.IsEmpty() << endl;
cout << "Peek " << cs.Peek() << endl;
cs.Push(2);
cout << "IsEmpty " << cs.IsEmpty() << endl;
cout << "Peek " << cs.Peek() << endl;
cs.Push(3);
cout << "IsEmpty " << cs.IsEmpty() << endl;
cout << "Peek " << cs.Peek() << endl;
cs.Pop();
cout << "IsEmpty " << cs.IsEmpty() << endl;
cout << "Peek " << cs.Peek() << endl;
cs.Clear();
cout << "IsEmpty " << cs.IsEmpty() << endl;
}

输出

IsEmpty 1
IsEmpty 0
Peek 1
IsEmpty 0
Peek 2
IsEmpty 0
Peek 3
IsEmpty 0
Peek 2
IsEmpty 1

关于c++ - 如何在模板类中使用唯一指针深度复制构造函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19238681/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com