How to convert from raw pointer to unique pointer in C++ -


so, thought converting raw pointer unique pointer not hard @ all. however, trying attempt 1 myself, run lot of problems don't know of. example below explain more mean. got lot of errors visual studio don't know how fix it. can tell me 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

#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

#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

#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 using swap function swap content between pointers.

here errors 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

unique_ptr has empty constructor , nullptr constructor, says nothing 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 want use void swap(unique_ptr& other) swap pointers between unique_ptrs without deconstructing them happen operator=. try again in mind, should take @ cppreference.com unique_ptr page understand how works.

on second note linked lists if i'd use raw pointers.


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -