我创建了双链表的ADT版本,并负责使用链表而不是数组来重做旧实验室。我需要在链接列表上执行冒泡排序。我以为我会尝试使用迭代器来做到这一点。在对冒泡排序进行编码并将函数从源代码移动到链接列表本身的过程中的某个地方,我开始遇到这些未解决的外部符号错误。
我尝试将冒泡排序功能移回源代码,但错误仍然存在。我在这里发现了很多有关这些错误的示例,但是到目前为止,我发现没有任何帮助。我的定义或声明没有任何问题。据我所知,没有遗漏的括号。
以下是出现错误的文件:
Source.cpp
#include <iostream>
#include <string>
#include "Person.h"
#include "DoublyLinkedList.h"
#include "LinkedListIterator.h"
using namespace std;
void bubbleSort(doublyLinkedList<Person>& list);
int main()
{
doublyLinkedList<Person> people;
int isDone = false;
//Loop until the User enters "-1" as a name
while (!isDone)
{
string name = "";
int age = 0;
//Prompt user for Name and Age and save them to the people array
cout << "Enter name (-1 to stop): ";
cin >> name;
if (name == "-1")
{
isDone = true;
}
else
{
cout << "Enter age of " << name << ": ";
cin >> age;
people.insert(Person(name, age));
}
}
cout << endl;
//Sort Person Objects by Age
bubbleSort(people);
//Display Names and Ages to User
for (int i = 0; i < people.length(); i++)
{
people.print();
}
system("pause");
return 0;
}
template<class Type>
void bubbleSort(doublyLinkedList<Person> &list)
{
bool sorted = false;
while (!sorted)
{
sorted = true;
if (list.length() < 2)
{
return;
}
linkedListIterator<Person> iter = list.begin();
linkedListIterator<Person> iter_prev = list.begin();
iter++;
while (iter != list.end())
{
if ((*iter).getAge() > (*iter_prev).getAge())
{
sorted = false;
Person temp = (*iter);
(*iter) = (*iter_prev);
(*iter_prev) = temp;
}
iter++;
iter_prev++;
}
}
}
DoublyLinkedList.h(仅显示错误的函数)
void print() const;
int length() const;
void insert(const Type& insertItem);
doublyLinkedList();
~doublyLinkedList();
DoublyLinkedList.cpp
#include <iostream>
#include <cassert>
#include "DoublyLinkedList.h"
template <class Type>
doublyLinkedList<Type>::doublyLinkedList()
{
first = nullptr;
last = nullptr;
count = 0;
}
template <class Type>
bool doublyLinkedList<Type>::isEmptyList() const
{
return (first == nullptr);
}
template <class Type>
int doublyLinkedList<Type>::length() const
{
return count;
}
template <class Type>
void doublyLinkedList<Type>::print() const
{
nodeType<Type> *current; //pointer to traverse the list
current = first; //set current to point to the first node
while (current != nullptr)
{
cout << current->info << " "; //output info
current = current->next;
}//end while
}
template <class Type>
void doublyLinkedList<Type>::insert(const Type& insertItem)
{
nodeType<Type> *current; //pointer to traverse the list
nodeType<Type> *trailCurrent; //pointer just before current
nodeType<Type> *newNode; //pointer to create a node
bool found;
newNode = new nodeType<Type>; //create the node
newNode->info = insertItem; //store the new item in the node
newNode->next = nullptr;
newNode->back = nullptr;
if (first == nullptr) //if the list is empty, newNode is
//the only node
{
first = newNode;
last = newNode;
count++;
}
else
{
found = false;
current = first;
while (current != nullptr && !found) //search the list
if (current->info >= insertItem)
found = true;
else
{
trailCurrent = current;
current = current->next;
}
if (current == first) //insert newNode before first
{
first->back = newNode;
newNode->next = first;
first = newNode;
count++;
}
else
{
//insert newNode between trailCurrent and current
if (current != nullptr)
{
trailCurrent->next = newNode;
newNode->back = trailCurrent;
newNode->next = current;
current->back = newNode;
}
else
{
trailCurrent->next = newNode;
newNode->back = trailCurrent;
last = newNode;
}
count++;
}//end else
}//end else
}//end insert
template <class Type>
doublyLinkedList<Type>::~doublyLinkedList()
{
cout << "Definition of the destructor is left as an exercise." << endl;
cout << "See Programming Execrise 9." << endl;
}
运行代码时,我得到了:
1> ------开始构建:项目:Lab11,配置:Debug Win32 ------ 1> DoublyLinkedList.cpp 1> Source.obj:错误LNK2019:在函数_main中引用的未解析的外部符号“ void __cdecl bubbleSort(class doublyLinkedList&)”(?bubbleSort @@ YAXAAV?$ doublyLinkedList @ VPerson @@@@@ Z) 1> Source.obj:错误LNK2019:未解析的外部符号“ public:void __thiscall doublyLinkedList :: print(void)const”(?print @?$ doublyLinkedList @ VPerson @@@@@ QBEXXZ)在函数_main中引用 1> Source.obj:错误LNK2019:未解析的外部符号“ public:int __thiscall doublyLinkedList :: length(void)const”(?length @?$ doublyLinkedList @ VPerson @@@@@ QBEHXZ)在函数_main中引用 1> Source.obj:错误LNK2019:未解析的外部符号“公共:无效__thiscall doublyLinkedList :: insert(class Person const&)”(?insert @?$ doublyLinkedList @ VPerson @@@@@ QAEXABVPerson @@@@ Z) _主要 1> Source.obj:错误LNK2019:未解决的外部符号“ public:__ thiscall doublyLinkedList :: doublyLinkedList(void)”(?? 0?$ doublyLinkedList @ VPerson @@@@@ QAE @ XZ)在函数_main中引用 1> Source.obj:错误LNK2019:未解析的外部符号“ public:__ thiscall doublyLinkedList ::〜doublyLinkedList(void)”(?? 1?$ doublyLinkedList @ VPerson @@@@@ QAE @ XZ)在函数_main中引用 1> E:\ Documents \ WakeTech \ CSC234 \ Unit11 \ Lab11 \ Debug \ Lab11.exe:致命错误LNK1120:6个未解决的外部组件 1>完成的建筑项目“ Lab11.vcxproj”-失败。 ===========构建:0成功,1失败,0最新,跳过0 ==========