SqList.h头文件
#define DEFAULT_SIZE 100
#include<iostream>
template<class ElemType>
class SqList
{
protected:
int count;
int maxSize;
ElemType *elems;
public:
SqList(int size = DEFAULT_SIZE);
virtual ~SqList();
int Length() const;
bool Empty() const;
void Clear();
void Traverse(void(*visit)(const ElemType &))const;
bool GetElem(int position, ElemType &e) const;
bool SetElem(int position, const ElemType &e);
bool Delete(int position, ElemType &e);
bool Insert(int position, const ElemType &e);
SqList(const SqList<ElemType> ©); //复制构造函数
SqList<ElemType>& operator=(const SqList<ElemType>& copy);
void Show();
};
template<class ElemType>
void Difference(const SqList<ElemType> &la, const SqList<ElemType> &lb, SqList<ElemType> &lc);
cpp实现文件
#include "SqList.h"
template<class ElemType>
SqList<ElemType>::SqList(int size)
{
maxSize = size;
elems = new ElemType[maxsize];
count = 0;
}
template<class ElemType>
SqList<ElemType>::~SqList()
{
delete[] elems;
}
template<class ElemType>
int SqList<ElemType>::Length()const
{
return count;
}
template<class ElemType>
bool SqList<ElemType>::Empty() const
{
return count == 0;
}
template<class ElemType>
void SqList<ElemType>::Clear()
{
count = 0;
}
template<class ElemType>
void SqList<ElemType>::Traverse(void(*visit)(const ElemType &))const
{
for (int pos = 1; pos <= count; pos++)
(*void)(elems[pos - 1]);
}
template<class ElemType>
bool SqList<ElemType>::GetElem(int position, ElemType &e)const
{
if (position<1 || position>count)
{
return false;
}
else
{
e = elems[position - 1];
return true;
}
}
template<class ElemType>
bool SqList<ElemType>::SetElem(int position, const ElemType &e)
{
if (position<1 || position>count)
{
return false;
}
else
{
elems[position - 1] = e;
return true;
}
}
template<class ElemType>
bool SqList<ElemType>::Insert(int position, const ElemType &e)
{
if (count == maxSize)
return false;
else if (position<1 || position>count + 1)
return false;
else
{
count++;
for (int i = count; i >= position ; i--) //count先进行自加运算,所以在末尾处插入新值没有bug;
{
elems[i] = elems[i-1];
}
elems[position-1] = e;
return true;
}
}
template<class ElemType>
bool SqList<ElemType>::Delete(int position, ElemType &e)
{
if (position<1 || position>count)
return false;
else
{
GetElem(position, e);
for (int pos = position; pos <count; pos++)
{
elems[pos - 1] = elems[pos];
}
count--;
return true;
}
}
template<class ElemType>
SqList<ElemType>::SqList(const SqList<ElemType> ©)
{
maxSize = copy.maxSize;
elems = new ElemType[maxSize];
count = copy.count;
for (int i = 0; i <= count - 1; i++)
{
elems[i] = copy.elems[i];
}
}
template<class ElemType>
SqList<ElemType>& SqList<ElemType>::operator=(const SqList<ElemType>& copy)
{
if (© != this)
{
maxSize = copy.maxSize;
if (elems != NULL) delete []elems;
elems = new ElemType[maxSize];
count = copy.count;
for (int i = 0; i < count; i++)
{
elems[i] = copy.elems[i];
}
return true;
}
}
template<class ElemType>
void SqList<ElemType>::Show()
{
for (int i = 0; i < count; i++)
{
cout << elems[i] << " ";
}
cout << endl;
}
template<class ElemType>
void Difference(const SqList<ElemType> &la,const SqList<ElemType> &lb, SqList<ElemType> &lc)
{
ElemType aelem, belem;
bool isExist = false;
lc.Clear();
for (int apos = 1; apos <= la.Length(); apos++)
{
la.GetElem(apos, aelem);
isExist = false;
for (int bpos = 1; bpos <= lb.Length(); bpos++)
{
lb.GetElem(bpos, belem);
if (aelem == belem)
{
isExist = true;
break;
}
}
if (!isExist)
{
lc.Insert(lc.Length() + 1, aelem);
}
}
}
下面是编译出现的问题:
1>------ 已启动生成: 项目: 头文件, 配置: Debug Win32 ------
1> SqList.cpp
1>测试.obj : error LNK2019: 无法解析的外部符号 "public: __thiscall SqList::SqList(int)" (??0?$SqList@H@@QAE@H@Z),该符号在函数 _main 中被引用
1>测试.obj : error LNK2019: 无法解析的外部符号 "public: virtual __thiscall SqList::~SqList(void)" (??1?$SqList@H@@UAE@XZ),该符号在函数 _main 中被引用
1>测试.obj : error LNK2019: 无法解析的外部符号 "public: bool __thiscall SqList::Insert(int,int const &)" (?Insert@?$SqList@H@@QAE_NHABH@Z),该符号在函数 _main 中被引用
1>测试.obj : error LNK2019: 无法解析的外部符号 "public: void __thiscall SqList::Show(void)" (?Show@?$SqList@H@@QAEXXZ),该符号在函数 _main 中被引用
1>C:\Users\Park\Desktop\数据结构笔记\绪论\头文件\Debug\头文件.exe : fatal error LNK1120: 4 个无法解析的外部命令
========== 生成: 成功 0 个,失败 1 个,最新 0 个,跳过 0 个 ==========