使用 std::unique_ptr 的通用单链表,Visual Studio C++ Microsoft中存在未知的反



我对Visual Studio C++以及std::unique_ptr Microsoft很陌生。在CodeReview上,我被建议使用std::unique_ptr重写。你可以在这里找到我引用的问题。

以下是我收到的以下错误:

1>main.cpp
1>c:devlinkedlistlinkedlistsinglelinkedlist.h(26): error C2760: syntax error: unexpected token 'identifier', expected ';'
1>c:devlinkedlistlinkedlistsinglelinkedlist.h(61): note: see reference to class template instantiation 'SingleLinkedList<T>' being compiled
1>c:devlinkedlistlinkedlistsinglelinkedlist.h(65): error C2760: syntax error: unexpected token 'identifier', expected ';'
1>c:devlinkedlistlinkedlistsinglelinkedlist.h(104): error C2760: syntax error: unexpected token 'identifier', expected ';'
1>c:devlinkedlistlinkedlistsinglelinkedlist.h(104): error C7510: 'make_unique': use of dependent type name must be prefixed with 'typename'
1>c:devlinkedlistlinkedlistsinglelinkedlist.h(120): error C2760: syntax error: unexpected token 'identifier', expected ';'
1>c:devlinkedlistlinkedlistsinglelinkedlist.h(120): error C7510: 'make_unique': use of dependent type name must be prefixed with 'typename'
1>c:devlinkedlistlinkedlistsinglelinkedlist.h(136): error C2760: syntax error: unexpected token 'identifier', expected ';'
1>c:devlinkedlistlinkedlistsinglelinkedlist.h(136): error C7510: 'make_unique': use of dependent type name must be prefixed with 'typename'
1>c:devlinkedlistlinkedlistsinglelinkedlist.h(145): error C2760: syntax error: unexpected token 'identifier', expected ';'
1>c:devlinkedlistlinkedlistsinglelinkedlist.h(145): error C7510: 'make_unique': use of dependent type name must be prefixed with 'typename'
1>c:devlinkedlistlinkedlistsinglelinkedlist.h(152): error C2760: syntax error: unexpected token 'identifier', expected ';'
1>c:devlinkedlistlinkedlistsinglelinkedlist.h(152): error C7510: 'make_unique': use of dependent type name must be prefixed with 'typename'
1>c:devlinkedlistlinkedlistsinglelinkedlist.h(164): error C2760: syntax error: unexpected token 'identifier', expected ';'
1>c:devlinkedlistlinkedlistsinglelinkedlist.h(164): error C7510: 'make_unique': use of dependent type name must be prefixed with 'typename'
1>Done building project "LinkedList.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

这是头文件:

#ifndef SingleLinkedList_h
#define SingleLinkedList_h
#include <iostream>
template <class T>
class SingleLinkedList {
private:
struct Node {
T data;
std::unique_ptr<Node> next = nullptr;
Node(T x) : data(x), next(nullptr) {}
};
std::unique_ptr<Node> head = nullptr;
std::unique_ptr<Node> tail = nullptr;
// This function is for the overloaded operator << 
void display(std::ostream &str) const {
for (std::make_unique<Node> loop = head; loop != nullptr; loop = loop->next) {
str << loop->data << "t";
}
str << "n";
}
public:
// Constructors
SingleLinkedList() = default;                                           // empty constructor 
SingleLinkedList(SingleLinkedList const &source);                       // copy constructor
// Rule of 5
SingleLinkedList(SingleLinkedList &&move) noexcept;                     // move constructor
SingleLinkedList& operator=(SingleLinkedList &&move) noexcept;          // move assignment operator
~SingleLinkedList();                                    
// Overload operators
SingleLinkedList& operator=(SingleLinkedList const &rhs);
friend std::ostream& operator<<(std::ostream &str, SingleLinkedList &data) {
data.display(str);
return str;
}
// Memeber functions
void swap(SingleLinkedList &other) noexcept;
void push(const T &theData);                            
void push(T &&theData);
void display() const;
void insertHead(const T &theData);
void insertTail(const T &theData);
void insertPosition(int pos, const T &theData);
void deleteHead();
void deleteTail();
void deletePosition(int pos);
bool search(const T &x);
};
template <class T>
SingleLinkedList<T>::SingleLinkedList(SingleLinkedList<T> const &source) {
for(std::make_unique<Node> loop = source->head; loop != nullptr; loop = loop->next) {
push(loop->data);
}
}
template <class T>
SingleLinkedList<T>::SingleLinkedList(SingleLinkedList<T>&& move) noexcept {
move.swap(*this);
}
template <class T>
SingleLinkedList<T>& SingleLinkedList<T>::operator=(SingleLinkedList<T> &&move) noexcept {
move.swap(*this);
return *this;
}
template <class T>
SingleLinkedList<T>::~SingleLinkedList() {
while (head != nullptr) {
deleteHead();
}
}
template <class T>
SingleLinkedList<T>& SingleLinkedList<T>::operator=(SingleLinkedList const &rhs) {
SingleLinkedList copy{ rhs };
swap(copy);
return *this;
}
template <class T>
void SingleLinkedList<T>::swap(SingleLinkedList &other) noexcept {
using std::swap;
swap(head, other.head);
swap(tail, other.tail);
}
template <class T>
void SingleLinkedList<T>::push(const T &theData) {
std::make_unique<Node> newNode = Node(theData);
if (head == nullptr) {
head = newNode;
tail = newNode;
newNode = nullptr;
}
else {
tail->next = newNode;
tail = newNode;
}
}
template <class T>
void SingleLinkedList<T>::push(T &&theData) {
std::make_unique<Node> newNode = Node(std::move(theData));
if (head == nullptr) {
head = newNode;
tail = newNode;
newNode = nullptr;
}
else {
tail->next = newNode;
tail = newNode;
}
}
template <class T>
void SingleLinkedList<T>::display() const {
std::make_unique<Node> newNode = head;
while (newNode != nullptr) {
std::cout << newNode->data << "t";
newNode = newNode->next;
}
}
template <class T>
void SingleLinkedList<T>::insertHead(const T &theData) {
std::make_unique<Node> newNode = Node(theData);
newNode->next = head;
head = newNode;
}
template <class T>
void SingleLinkedList<T>::insertTail(const T &theData) {
std::make_unique<Node> newNode = Node(theData);
tail->next = newNode;
tail = newNode;
}
template <class T>
void SingleLinkedList<T>::insertPosition(int pos, const T &theData) {
}
template <class T>
void SingleLinkedList<T>::deleteHead() {
std::make_unique<Node> old = head;
head = head->next;
delete old;
}
template <class T>
void SingleLinkedList<T>::deleteTail() {
}
template <class T>
void SingleLinkedList<T>::deletePosition(int pos) {
}
template <class T>
bool SingleLinkedList<T>::search(const T &x) {
}


#endif /* SingleLinkedList_h*/

这是主.cpp文件:

#include <algorithm>
#include <cassert>
#include <iostream>
#include <ostream>
#include <iosfwd>
#include "SingleLinkedList.h"

int main(int argc, const char * argv[]) {

///////////////////////////////////////////////////////////////////////
///////////////////////////// Single Linked List //////////////////////
///////////////////////////////////////////////////////////////////////
SingleLinkedList<int> obj;
obj.push(2);
obj.push(4);
obj.push(6);
obj.push(8);
obj.push(10);
std::cout<<"n--------------------------------------------------n";
std::cout<<"---------------displaying all nodes---------------";
std::cout<<"n--------------------------------------------------n";
std::cout << obj << std::endl;
//
//    std::cout<<"n--------------------------------------------------n";
//    std::cout<<"-----------------Inserting At End-----------------";
//    std::cout<<"n--------------------------------------------------n";
//    obj.insertTail(20);
//    std::cout << obj << std::endl;
//
//    std::cout<<"n--------------------------------------------------n";
//    std::cout<<"----------------Inserting At Start----------------";
//    std::cout<<"n--------------------------------------------------n";
//    obj.insertHead(50);
//    std::cout << obj << std::endl;
//
//    std::cout<<"n--------------------------------------------------n";
//    std::cout<<"-------------Inserting At Particular--------------";
//    std::cout<<"n--------------------------------------------------n";
//    obj.insertPosition(5,60);
//    std::cout << obj << std::endl;
//
//    std::cout<<"n--------------------------------------------------n";
//    std::cout<<"----------------Deleting At Start-----------------";
//    std::cout<<"n--------------------------------------------------n";
//    obj.deleteHead();
//    std::cout << obj << std::endl;
//
//    std::cout<<"n--------------------------------------------------n";
//    std::cout<<"----------------Deleting At End-----------------";
//    std::cout<<"n--------------------------------------------------n";
//    obj.deleteTail();
//    std::cout << obj << std::endl;
//
//
//    std::cout<<"n--------------------------------------------------n";
//    std::cout<<"--------------Deleting At Particular--------------";
//    std::cout<<"n--------------------------------------------------n";
//    obj.deletePosition(4);
//    std::cout << obj << std::endl;
//    std::cout << std::endl;
//
//    obj.search(8) ? printf("Yes"):printf("No");



std::cin.get();
}

我认为大多数错误都是语法错误,或者我只是犯了非常粗心的错误。谢谢。

你的大部分语法错误都是同一个错误的衍生物,所以让我们看看第一个。这是额外的指导,因为它指出了两个错误。任何时候你都可以用一只鸟杀死两块石头,我说去吧。在最好的时候杀死一块石头是该死的困难。

void display(std::ostream &str) const {
for (std::make_unique<Node> loop = head; loop != nullptr; loop = loop->next) {
str << loop->data << "t";
}
str << "n";
}

std::make_unique<Node> loop = head中,std::make_unique是一个给你一个std::unique_ptr的函数。它不是可用于声明变量的类型。这个错误在代码中重复了几次,清理所有这些错误都会发现一堆新错误。耶,好玩。

第一步,让我们将函数替换为正确的类型。

void display(std::ostream &str) const {
for (std::unique_ptr<Node> loop = head; loop != nullptr; loop = loop->next) {
str << loop->data << "t";
}
str << "n";
}

槽的。简单。不幸的是,它不起作用。std::unique_ptr<Node> loop = head意味着您现在对同一对象有两个unique_ptr。这听起来不是特别独特,编译器不允许这样做。您可能会收到一些有关已删除函数的晦涩错误消息。那是因为unique_ptr的复制构造函数和=运算符已被放逐到黑暗维度,以使其更难意外地以多个unique_ptr结束。

快速说明为什么这一切很重要:所有权。指针的最大问题之一是决定谁delete什么以及何时。为了解决这个问题,你建立了一个指针的所有权。也许指针指向一个自动变量,堆栈或任何用于管理自动数据的东西都会处理它。你什么都delete!唧唧哈!也许有人new了变量。必须有人delete它。有人是所有者。一些疯狂的傻瓜甚至可能malloc了这个物体。必须有人free它,而不是delete它。再次,这是所有者。

没有实用的方法来确定如何分配对象以及如何从指针释放对象。指针所做的只是点。程序员需要建立一个合同,概述谁(如果有的话)照顾它以及如何照顾它。在过去,这可能是一个艰苦的过程。

历史表明,基于合约的指针管理很难做到正确。如果你做对了,下一个程序员可以完全把它搞砸。当合约具有某种自动执行时,指针管理的效果要好得多。

今天我们使用智能指针。有一堆智能指针。这次我们坚持unique_ptr。如果您想了解更多信息,请阅读什么是智能指针,何时应该使用智能指针?

unique_ptr是业主。它是一个普通的旧 Automatic 变量,恰好包含一个指针,一旦unique_ptr超出范围,它就会释放该指针。不再担心所有权。所有者是明确的。就像汉兰达一样,只能有一个。如果你对同一个对象有两个unique_ptr,它并不是那么独特,不是吗?有两个所有者,其中一个会先超出范围并摧毁对象,给另一个所有者留下一个滴答作响的定时炸弹。

不要newunique_ptr.这完全违背了重点。

您无法复制unique_ptr,但可以使用std::move转让所有权。请注意,目标unique_ptr当前拥有的对象(如果有)将被销毁。

如果调用接收unique_ptr的函数,则必须将所有权转移到接收函数参数。指针现在将被销毁,函数返回,假设它不会将指针的所有权转移到其他地方。由于您通常不希望发生这种情况,但仍希望指针的所有权保持清晰,因此请通过引用传递unique_ptr

你也可以使用unique_ptr::get方法来获取一个原始的、无主的指针,但无论你把它交给谁,都必须知道不要管指针,这让我们回到了基于合约的指针管理。

哼。这不是那么快<咒骂删除>的全部,是吗?对此感到抱歉。

回到正题,你的指针归unique_ptr所有。你不能只用上面的std::unique_ptr<Node> loop = head;汉兰达定律制作一个新的副本。将所有权转移到一个范围很窄的 Automatic 变量,该变量将在循环结束时死亡并销毁指针,std::unique_ptr<Node> loop = std::move(head);将使head指针指向任何内容。没用。链表被毁了,但至少其中的所有内容都被"正确"销毁了。

如上所述,您可以使用引用或get。不能用loop = loop->next重新拔插引用,因此引用不起作用。这就剩下get

void display(std::ostream &str) const {
for (Node* loop = head.get(); loop != nullptr; loop = loop->next.get()) {
str << loop->data << "t";
}
str << "n";
}

程序员在这里被原始指针混淆的空间很小,所有权仍然由unique_ptr保证,所以一切都应该很好。

此问题在 中重复出现

template <class T>
SingleLinkedList<T>::SingleLinkedList(SingleLinkedList<T> const &source) {
for(std::make_unique<Node> loop = source->head; loop != nullptr; loop = loop->next) {
push(loop->data);
}
}

相同的解决方案。

两种push方法都有变体

template <class T>
void SingleLinkedList<T>::push(const T &theData) {
std::make_unique<Node> newNode = Node(theData);
if (head == nullptr) {
head = newNode;
tail = newNode;
newNode = nullptr;
}
else {
tail->next = newNode;
tail = newNode;
}
}

解决方案(某种):

template <class T>
void SingleLinkedList<T>::push(const T &theData) {
std::unique_ptr<Node> newNode = std::make_unique<Node>(theData);
if (head == nullptr) {
head = newNode;
tail = newNode;
newNode = nullptr;
}
else {
tail->next = newNode;
tail = newNode;
}
}

但请注意headtail的分配。这违反了汉兰达,需要修复。您可以将newNode的所有权转让给head,但没有什么可以给tail的了。tail为什么要拥有任何东西的所有权?无论tail指向什么,都应该归head或某些Nodenext所有。它的所有权是有保证的。由于它被限制在SingleLinkedList它正在与自己签订合同,并且可以侥幸成为一个普通的旧指针。

这意味着

Node* tail = nullptr;

template <class T>
void SingleLinkedList<T>::push(const T &theData) {
std::unique_ptr<Node> newNode = std::make_unique<Node>(theData);
if (head == nullptr) {
head = std::move(newNode);
tail = head.get();
}
else {
tail->next = std::move(newNode);
tail = tail->next.get();
}
}

我没时间了,但是

void SingleLinkedList<T>::insertHead(const T &theData)

void SingleLinkedList<T>::insertTail(const T &theData) {

需要更新到新的世界秩序并考虑到"如果列表为空怎么办?

最新更新