1.问题描述:
在学习数据结构的时候看到了一个线性表实现的实例,按照书上的代码编程后,出现了错误LNK2019,如图:
我的项目文件目录如下:
-SeqList
-SeqList.h
-SeqList.cpp
-PHONEBOOK.h
-PHONEBOOK.cpp
-Main.cpp
各文件的代码如下:
SeqList.h
#pragma once
template<class T, int N>
class SeqList {
public:
SeqList();
SeqList(const T a[], int n);
int GetLength();
void PrintList();
void Insert(int i, T x);
T Delete(int i);
T Get(int i);
int Locate(T x);
private:
T data[N];
int length;
};
SeqList.cpp
#include"SeqList.h"
#include<iostream>
using namespace std;
template<class T, int N>
SeqList<T, N>::SeqList() {
length = 0;
}
template<class T, int N>
SeqList<T, N>::SeqList(const T a[], int n) {
if (n > N) throw "The length of the array is out of the maximum length of the SeqList";
for (int i = 0; i < n; i++)
data[i] = a[i];
length = n;
}
template<class T, int N>
int SeqList<T, N>::GetLength() {
return length;
}
template<class T, int N>
void SeqList<T, N>::PrintList() {
cout << "Print every element of the SeqList in order: " << endl;
for (int i = 0; i < length; i++)
cout << data[i] << " ";
cout << endl;
}
template<class T, int N>
void SeqList<T, N>::Insert(int i, T x) {
if (length >= N) throw "Overflow Exception";
if (i < 1 || i >= length + 1) throw "Location Exception";
for (int j = length; j >= i; j--)
data[j] = data[j - 1];
data[i - 1] = x;
length++;
}
template<class T, int N>
T SeqList<T, N>::Delete(int i) {
if (0 == length) throw "Underflow Exception";
if (i<1 || i>length) throw "Location Exception";
T x = data[i - 1];
for (int j = i; j < length; j++)
data[j - 1] = data[j];
length--;
return x;
}
template<class T, int N>
T SeqList<T, N>::Get(int i) {
if (i<1 || i>length) throw "Location Exception";
return data[i - 1];
}
template<class T, int N>
int SeqList<T, N>::Locate(T x) {
for (int i = 0; i < length; i++)
if (data[i] == x) return i + 1;
return 0;
}
PHONEBOOK.h
#pragma once
#include<string>
using namespace std;
class PHONEBOOK {
private:
int m_ID;
string m_name;
string m_phone;
string m_group;
public:
PHONEBOOK();
PHONEBOOK(int id, const char* name, const char* phone, const char* group);
void print();
bool operator==(PHONEBOOK& p);
friend ostream& operator<<(ostream& output, PHONEBOOK& p);
};
PHONEBOOK.cpp
#include"PHONEBOOK.h"
#include<iostream>
using namespace std;
PHONEBOOK::PHONEBOOK() {
m_ID = 0;
m_name = "null";
m_phone = "null";
m_group = "null";
}
PHONEBOOK::PHONEBOOK(int id, const char* name, const char* phone, const char* group) {
m_ID = id;
m_name = name;
m_phone = phone;
m_group = group;
}
void PHONEBOOK::print() {
cout << m_ID << '\t' << m_name << '\t' << m_phone << '\t' << m_group << endl;
}
bool PHONEBOOK::operator==(PHONEBOOK& p) {
if (p.m_ID == m_ID)
return true;
return false;
}
ostream& operator<<(ostream& output, PHONEBOOK& p) {
output << p.m_ID << '\t' << p.m_name << '\t' << p.m_phone << '\t' << p.m_group;
return output;
}
Main.cpp
#include"SeqList.h"
#include"PHONEBOOK.h"
#include<iostream>
using namespace std;
int main() {
PHONEBOOK pbook[4] = {
{20181208,"Mary","13011221827","classmates"},
{20181127,"Tom","13934621123","family"},
{20181156,"John","1324579880","classmates"},
{20181133,"Lisa","1378001822","classmates"}, };
PHONEBOOK record(20181209, "phoenix", "15930209020", "teacher");
SeqList<PHONEBOOK, 100>list(pbook, 4);
cout << "The list of the Phonebook:" << endl;
list.PrintList();
list.Insert(1, record);
cout << "The list of the Phonebook:" << endl;
list.PrintList();
PHONEBOOK x = list.Delete(3);
cout << "The deleted element:" << endl;
x.print();
cout << "The list of the Phonebook:" << endl;
list.PrintList();
int p = list.Locate(record);
cout << "The location of 'phoenix' is " << p << endl;
return 0;
}
2.问题解决
关于LNK2019错误的原因有很多,详见Visual Studio的官方文档。我的这种情况主要原因是模板类SeqList的声明和定义分成两个文档,这会导致编译通过不了。因此我将SeqList类的声明和定义均放到SeqList.h文件中,解决了问题。
关于模板类的声明和定义能否分成两个文档,可以参考知乎的一个问答:https://www.zhihu.com/question/20630104