如何在 C++ 中将原始指针转换为唯一指针

Hoa*_*inh 5 c++ pointers unique

所以,我认为从原始指针转换为唯一指针一点也不难。然而,当我尝试为自己做一件事时,我遇到了很多我什至不知道的问题。下面的这个例子将更多地解释我的意思。我从 Visual Studio 中收到很多错误,但我不知道如何修复它。谁能告诉我我做错了什么吗?谢谢

Contact.h

#pragma once
#include<iostream>
#include<string>
#include<memory>

class Contact
{
    friend std::ostream& operator<<(std::ostream& os, const Contact& c);
    friend class ContactList;

public:
    Contact(std::string name = "none");

private:
    std::string name;
    //Contact* next;    
    std::unique_ptr<Contact> next;
};
Run Code Online (Sandbox Code Playgroud)

联系方式.cpp

#include"Contact.h"

using namespace std;

Contact::Contact(string n):name(n), next(new Contact())
{
}

ostream& operator<<(ostream& os, const Contact& c)
{
    return os << "Name: " << c.name;
}
Run Code Online (Sandbox Code Playgroud)

联系人列表.h

#pragma once
#include"Contact.h"
#include<memory>

using namespace std;

class ContactList
{
public:
    ContactList();
    ~ContactList();
    void addToHead(const std::string&);
    void PrintList();

private:
    //Contact* head;
    unique_ptr<Contact> head;
    int size;
};
Run Code Online (Sandbox Code Playgroud)

联系人列表.cpp

#include"ContactList.h"
#include<memory>

using namespace std;

ContactList::ContactList(): head(new Contact()), size(0)
{
}

void ContactList::addToHead(const string& name)
{
    //Contact* newOne = new Contact(name);
    unique_ptr<Contact> newOne(new Contact(name));

    if(head == 0)
    {
        head.swap(newOne);
        //head = move(newOne);
    }
    else
    {
        newOne->next.swap(head);
        head.swap(newOne);
        //newOne->next = move(head);
        //head = move(newOne);
    }
    size++;
}

void ContactList::PrintList()
{
    //Contact* tp = head;
    unique_ptr<Contact> tp(new Contact());
    tp.swap(head);
    //tp = move(head);

    while(tp != 0)
    {
        cout << *tp << endl;
        tp.swap(tp->next);
        //tp = move(tp->next);
    }
}
Run Code Online (Sandbox Code Playgroud)

我通过使用交换函数来交换指针之间的内容进行更新。

这是我收到的所有错误

Error   2   error LNK1120: 1 unresolved externals   E:\Fall 2013\CPSC 131\Practice\Practice\Debug\Practice.exe  1
Run Code Online (Sandbox Code Playgroud)

错误 1 ​​错误 LNK2019:无法解析的外部符号“public: __thiscall ContactList::~ContactList(void)”(??1ContactList@@QAE@XZ) 在函数“public: void * __thiscall ContactList::`标量删除析构函数”(unsigned) 中引用int)" (??_GContactList@@QAEPAXI@Z) E:\2013 年秋季\CPSC 131\Practice\Practice\Practice\ContactListApp.obj

Edw*_*d A 3

unique_ptr有空构造函数和 nullptr 构造函数,它没有提到 0。

constexpr unique_ptr();
constexpr unique_ptr( nullptr_t );
explicit unique_ptr( pointer p );
unique_ptr( pointer p, /* see below */ d1 );
unique_ptr( pointer p, /* see below */ d2 );
unique_ptr( unique_ptr&& u );
template< class U, class E >
unique_ptr( unique_ptr<U, E>&& u );
template< class U >
unique_ptr( auto_ptr<U>&& u );
Run Code Online (Sandbox Code Playgroud)

另外,您将希望使用void swap(unique_ptr& other)在 unique_ptr 之间交换指针,而不需要像operator=. 记住所有这些,再试一次,您应该仔细查看cppreference.com unique_ptr页面以了解它是如何工作的。

关于链接列表的第二个注释,如果我是你,我会使用原始指针。