template 不能分别在.h和.cpp中定义模板
先上代码:
1 #ifndef SEQLIST_H 2 #define SEQLIST_H 3 4 #include <iostream> 5 6 7 const int MaxLength = 10; 8 9 template <typename type> 10 class SeqList 11 { 12 public: 13 SeqList(); 14 ~SeqList(); 15 int getLength() const; 16 type getElement(int i) const; 17 int locate(type &item) const; 18 int insert(const type item,int i); 19 type deleteElement(const int i); 20 bool isEmpty() const; 21 void clear(); 22 int append(type item); 23 private: 24 type data; 25 int length; 26 }; 27 28 #endif // SEQLIST_H
以上是数据结构中定义的顺序线性表的头文件,接下来的是类的实现过程,我是直接用的Qt做的,比较省事嘛。
1 #include "seqlist.h" 2 3 using namespace std; 4 5 //构造函数 6 template <typename type> SeqList<type>::SeqList():length(0) 7 { 8 data = new type[MaxLength]; 9 } 10 11 //析构函数 12 template <typename type> SeqList<type>::~SeqList() 13 { 14 delete []data; 15 return length; 16 } 17 18 //得到链表当前长度 19 template <typename type> int SeqList<type>::getLength() const 20 { 21 return length; 22 } 23 24 //根据索引得到相应元素 25 template <typename type> type SeqList<type>::getElement(int i) const 26 { 27 if(i>=0&&i<length) 28 return data[i]; 29 return NULL; 30 } 31 32 //根据元素得到索引位置 33 template <typename type> int SeqList<type>::locate(type &item) const 34 { 35 for(int i = 0;i<length;i++) 36 { 37 if(data[i]==item) 38 return i; 39 else 40 { 41 break; 42 } 43 } 44 } 45 46 //在第i个元素之前插入元素item 47 template <typename type> int SeqList<type>::insert(const type item, int i) 48 { 49 if(length == MaxLength) 50 return 0; 51 if(i<0||i>length) 52 return 0; 53 for(int j = length;j>i;j--) 54 { 55 data[j]= data[j-1]; 56 } 57 data[i] = item; 58 length++; 59 return 1; 60 } 61 62 //删除第i个元素 63 template <typename type> type SeqList<type>::deleteElement(const int i) 64 { 65 if(length == 0) 66 return 0; 67 if(i<0||i>length) 68 return 0; 69 type t = data[i]; 70 for(int j = i;j<length;j++) 71 { 72 data[j] = data[j+1]; 73 } 74 length --; 75 return t; 76 } 77 78 //查询链表是否为空 79 template <typename type> bool SeqList<type>::isEmpty() const 80 { 81 if(length == 0) 82 { 83 return true; 84 } 85 return false; 86 } 87 88 //清空链表 89 template <typename type> void SeqList<type>::clear() 90 { 91 length =0; 92 } 93 94 //添加一个元素 95 template <typename type>int SeqList<type>::append(type item) 96 { 97 if(length == MaxLength) 98 return 0; 99 data[length] = item; 100 return 1; 101 }
代码中如果存在错误,就是我才学数据结构,自己在实验室做练习时写错了。毕竟才学。
然后是main.cpp
1 #include "seqlist.h" 2 #include <iostream> 3 4 using namespace std; 5 6 int main() 7 { 8 SeqList<int> *list = new SeqList<int>; 9 for(int i = 0;i<5;i++) 10 { 11 list->insert(i,i); 12 } 13 14 for(int i = 0;i<5;i++) 15 { 16 list->getElement(i); 17 } 18 list->append(1); 19 return 0; 20 }
然后事情就发生了,我使用Qt已经有一段时间了,对Qt来说不说炉火纯青,但也了解不少了,怎么出现这种情况了呢?
error: LNK2019: 无法解析的外部符号 "public: int __thiscall SeqList<int>::append(int)" (?append@?$SeqList@H@@QAEHH@Z),该符号在函数 _main 中被引用
error: LNK2019: 无法解析的外部符号 "public: __thiscall SeqList<int>::SeqList<int>(void)" (??0?$SeqList@H@@QAE@XZ),该符号在函数 _main 中被引用
赚到VS上还是不行,想了会想起来应该是类中定义的方法未实现,于是把.cpp中的方法全部复制到了.h中,删掉.cpp。OK了
原来template 定义模板是不能分别在.h和.cpp中写的。