0
我試圖通過使用唯一指針來創建鏈接列表。但是,我的程序不能編譯,因爲我不知道如何解決它的一些奇怪的錯誤。誰會請幫我解決這個問題?謝謝。鏈接列表中的唯一指針
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);
}
}
這些都是我已經得到了錯誤:
Error 1 error LNK2019: unresolved external symbol "public: __thiscall ContactList::~ContactList(void)" ([email protected]@[email protected]) referenced in function "public: void * __thiscall ContactList::`scalar deleting destructor'(unsigned int)" ([email protected]@[email protected]) 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
你的代碼*不*編譯。它不*鏈接*。這些錯誤並不奇怪,(模板編譯錯誤很奇怪)。學習閱讀你的編譯器和你的鏈接器的輸出是學習C++和編寫代碼一樣的一部分。 – Johnsyweb
@Johnsyweb:謝謝,我想這需要時間和大量的練習來學習如何閱讀你的編譯器 –