Hoang Minh
Hoang Minh

Reputation: 1220

Unique pointer in Linked List

I was trying to create a Linked List by using unique pointer. However, my program does not compile due to some weird errors that I do not know how to fix it. Would anyone please help me out how to solve this 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 are the errors that 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

Upvotes: 0

Views: 560

Answers (1)

ComicSansMS
ComicSansMS

Reputation: 54589

Your ContactList destructor has no implementation.

Add to ContactList.cpp

ContactList::~ContactList()
{
}

Or (since the destructor is trivial anyway), just remove the explicit destructor from the class definition:

class ContactList
{
public:
    ContactList();
    // no explicit destructor required
    void addToHead(const std::string&);
    void PrintList();

private:
    unique_ptr<Contact> head;
    int size;
};

Upvotes: 4

Related Questions