简体   繁体   English

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

[英]How to convert from raw pointer to unique pointer in C++

So, I thought that by converting from raw pointer to unique pointer was not hard at all. 因此,我认为通过从原始指针转换为唯一指针一点都不困难。 However, as I am trying to attempt to do one for myself, I run into a lot of problems that I don't even know of. 但是,当我试图为自己做一个时,我遇到了很多我什至不知道的问题。 This example below will explain more what I mean. 下面的示例将解释我的意思。 I got a lot of errors from Visual Studio that I don't know how to fix it. 我从Visual Studio中收到很多错误,但我不知道如何解决。 Can I anyone tell me what I did wrong ? 我可以告诉我我做错了什么吗? Thank you 谢谢

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;
};

Contact.cpp Contact.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;
}

ContactList.h ContactList.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;
};

ContactList.cpp ContactList.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);
    }
}

I updated by using swap function to swap the content between pointers. 我通过使用swap函数在指针之间交换内容进行了更新。

Here is all the errors that I get 这是我得到的所有错误

Error   2   error LNK1120: 1 unresolved externals   E:\Fall 2013\CPSC 131\Practice\Practice\Debug\Practice.exe  1

Error 1 error LNK2019: unresolved external symbol "public: __thiscall ContactList::~ContactList(void)" (??1ContactList@@QAE@XZ) referenced in function "public: void * __thiscall ContactList::`scalar deleting destructor'(unsigned int)" (??_GContactList@@QAEPAXI@Z) E:\\Fall 2013\\CPSC 131\\Practice\\Practice\\Practice\\ContactListApp.obj 错误1错误LNK2019:未解析的外部符号“ public:__thiscall ContactList ::〜ContactList(void)”(?? 1ContactList @@ QAE @ XZ)在函数“ public:void * __thiscall ContactList ::`标量删除析构函数”中引用(未签名) int)”(?? _ GContactList @@ QAEPAXI @ Z)E:\\ Fall 2013 \\ CPSC 131 \\ Practice \\ Practice \\ Practice \\ ContactListApp.obj

unique_ptr has empty constructor and nullptr constructor, it says nothing about 0. 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 );

also, you're going to want to use void swap(unique_ptr& other) to swap pointers between unique_ptrs without deconstructing them as would happen with the operator= . 同样,您将要使用void swap(unique_ptr& other)在unique_ptrs之间交换指针,而又不像对operator=那样进行解构。 Try again with all that in mind, you should take a good look at the cppreference.com unique_ptr page to understand how it works. 请记住所有内容再试一次,您应该仔细阅读cppreference.com unique_ptr页面以了解其工作原理。

On a second note with linked lists if I were you I'd use raw pointers. 关于带有链接列表的第二个注释,如果您是我,那么我将使用原始指针。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM