c++ - Unique pointer in Linked List -
i trying create linked list using unique pointer. however, program not compile due weird errors not know how fix it. please me out how solve problem ? thank you.
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); } }
these errors i've got:
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 error 2 error lnk1120: 1 unresolved externals e:\fall 2013\cpsc 131\practice\practice\debug\practice.exe 1
your contactlist
destructor has no implementation.
add contactlist.cpp
contactlist::~contactlist() { }
or (since destructor trivial anyway), remove explicit destructor class definition:
class contactlist { public: contactlist(); // no explicit destructor required void addtohead(const std::string&); void printlist(); private: unique_ptr<contact> head; int size; };
Comments
Post a Comment