title: How to convert from raw pointer to unique pointer in C++
body: <p>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. Can I anyone tell me what I did wrong ? Thank you</p>
  
  <pre><code>Contact.h
  
  #pragma once
  #include&lt;iostream&gt;
  #include&lt;string&gt;
  #include&lt;memory&gt;
  
  class Contact
  {
      friend std::ostream&amp; operator&lt;&lt;(std::ostream&amp; os, const Contact&amp; c);
      friend class ContactList;
  
  public:
      Contact(std::string name = "none");
  
  private:
      std::string name;
      //Contact* next;    
      std::unique_ptr&lt;Contact&gt; next;
  };
  </code></pre>
  
  <p>Contact.cpp</p>
  
  <pre><code>#include"Contact.h"
  
  using namespace std;
  
  Contact::Contact(string n):name(n), next(new Contact())
  {
  }
  
  ostream&amp; operator&lt;&lt;(ostream&amp; os, const Contact&amp; c)
  {
      return os &lt;&lt; "Name: " &lt;&lt; c.name;
  }
  </code></pre>
  
  <p>ContactList.h</p>
  
  <pre><code>#pragma once
  #include"Contact.h"
  #include&lt;memory&gt;
  
  using namespace std;
  
  class ContactList
  {
  public:
      ContactList();
      ~ContactList();
      void addToHead(const std::string&amp;);
      void PrintList();
  
  private:
      //Contact* head;
      unique_ptr&lt;Contact&gt; head;
      int size;
  };
  </code></pre>
  
  <p>ContactList.cpp</p>
  
  <pre><code>#include"ContactList.h"
  #include&lt;memory&gt;
  
  using namespace std;
  
  ContactList::ContactList(): head(new Contact()), size(0)
  {
  }
  
  void ContactList::addToHead(const string&amp; name)
  {
      //Contact* newOne = new Contact(name);
      unique_ptr&lt;Contact&gt; newOne(new Contact(name));
  
      if(head == 0)
      {
          head.swap(newOne);
          //head = move(newOne);
      }
      else
      {
          newOne-&gt;next.swap(head);
          head.swap(newOne);
          //newOne-&gt;next = move(head);
          //head = move(newOne);
      }
      size++;
  }
  
  void ContactList::PrintList()
  {
      //Contact* tp = head;
      unique_ptr&lt;Contact&gt; tp(new Contact());
      tp.swap(head);
      //tp = move(head);
  
      while(tp != 0)
      {
          cout &lt;&lt; *tp &lt;&lt; endl;
          tp.swap(tp-&gt;next);
          //tp = move(tp-&gt;next);
      }
  }
  </code></pre>
  
  <p>I updated by using swap function to swap the content between pointers.</p>
  
  <p>Here is all the errors that I get</p>
  
  <pre><code>Error   2   error LNK1120: 1 unresolved externals   E:\Fall 2013\CPSC 131\Practice\Practice\Debug\Practice.exe  1
  </code></pre>
  
  <p>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</p>