免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2456 | 回复: 3
打印 上一主题 下一主题

[C++] 求大侠来,我把以前写的链表改成支持模板,编译出现一堆未定义引用 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2014-06-10 11:10 |只看该作者 |倒序浏览
我把以前写的链表改成支持模板,编译出现一堆未定义引用是啥意思?
List<int>::push_front(int const&)’未定义的引用
。。。。。

list.h
  1. #ifndef LIST_H
  2. #define LIST_H 1
  3. #include<cstddef>
  4. template< typename T>
  5. class List{
  6.     struct Node{
  7.             T data;
  8.         Node* next;
  9.         Node(const T& d=T()):data(d),next(0){}//零初始化
  10.     };
  11.     Node* head;//头指针,用来保存头节点的地址
  12.     int len;
  13.     public:
  14.     List():head(NULL),len(0){}
  15.     void push_front(const T& d);//前插
  16.     List& push_back(const T& d);//尾插
  17.     int size()const;
  18.     Node*& getptr(int pos);//找链表中指向指定位置的指针
  19.     void insert(const T& d,int pos);//在任意位置插入
  20.     void travel()const;//遍历
  21.     void clear();//清空这个链表
  22.     ~List();
  23.     void erase(int pos);//有效位置为0~size()-1
  24.     void remove(const T& d);
  25.     int find(const T& d)const;
  26.     void set(int pos,const T& d);
  27.     bool empty()const{return head == 0;}
  28.     const T& front()const{if(empty())throw"空";return head->data;}
  29.     const T& back()const;
  30. };
  31. #endif
复制代码

论坛徽章:
0
2 [报告]
发表于 2014-06-10 11:12 |只看该作者
list.cpp
  1. #include<iostream>
  2. using namespace std;
  3. #include"list.h"

  4. template < typename T>
  5. void List<T>::push_front(const T&d){
  6.         insert(d,0);
  7. }
  8. template < typename T>
  9. List<T>& List<T>::push_back(const T& d){//尾插
  10.         insert(d,size());
  11.         return *this;
  12. }
  13. template < typename T>
  14. int List<T>::size()const{
  15.         return len;
  16. }
  17. template < typename T>
  18. typename List<T>::Node*& List<T>::getptr(int pos){//找链表中指向指定位置的指针
  19.         if(pos<0||pos>size()) pos = 0;
  20.         if(pos == 0)return head;
  21.         Node* p = head;
  22.         for(int i=1;i<pos;i++)
  23.                 p = p->next;
  24.         return (*p).next;
  25.         //1:return (*p).next;
  26.         //2:p=p->next:return (*p).next;
  27.         //3:p=p->next:p=p->next;return (*p).next;
  28. }
  29. template < typename T>
  30. void List<T>::insert(const T& d,int pos){//在任意位置插入
  31.         Node*& pn = getptr(pos);
  32.         Node* p = new Node(d);
  33.         p->next = pn;
  34.         pn = p;
  35.         len++;
  36. }
  37. template < typename T>
  38. void List<T>::travel()const{//遍历
  39.         Node* p = head;
  40.         while(p!=NULL){
  41.                 cout << p->data << ' ';
  42.                 p = p->next;
  43.         }
  44.         cout << endl;
  45. }
  46. template < typename T>
  47. void List<T>::clear(){//清空这个链表
  48.         while(head != NULL){
  49.                 Node* p = head->next;
  50.                 //        cout << "delete" << head << endl;
  51.                 delete head;
  52.                 head = p;
  53.         }
  54.         len = 0;
  55. }
  56. template < typename T>
  57. List<T>::~List<T>(){
  58.         //cout << this << "*********" << head << endl;
  59.         clear();
  60. }
  61. template < typename T>
  62. void List<T>::erase(int pos){//有效位置为0~size()-1
  63.         if(pos<0||pos>=size())return;
  64.         Node*& pn = getptr(pos);
  65.         Node* p = pn;
  66.         pn = pn->next;
  67.         delete p;
  68.         --len;
  69. }
  70. template < typename T>
  71. void List<T>::remove(const T& d){
  72.         int pos;
  73.         while((pos = find(d))!=-1)
  74.                 erase(pos);
  75. }
  76. template < typename T>
  77. int List<T>::find(const T& d)const{
  78.         Node* p = head;
  79.         int pos=0;
  80.         while(p){
  81.                 if(p->data==d)return pos;
  82.                 p = p->next;++pos;
  83.         }
  84.         return -1;
  85. }
  86. template < typename T>
  87. void List<T>::set(int pos,const T& d){
  88.         if(pos<0||pos>=size())return;
  89.         getptr(pos)->data = d;
  90. }
  91. template < typename T>
  92. const T& List<T>::back()const{
  93.         if(empty())throw"NULL";
  94.         Node* p=head;
  95.         while(p!=NULL&&p->next!=NULL){
  96.                 p=p->next;
  97.         }
  98.         return p->data;
  99. }
复制代码
main.cpp
  1. #include <iostream>
  2. using namespace std;
  3. #include "list.h"
  4. int main()
  5. {
  6.         List<int> l;
  7.         l.push_front(5);//5
  8.         l.push_front(8);//8 5
  9.         l.push_front(20);//20 8 5
  10.         l.insert(9,2);//20 8 9 5
  11.         l.insert(6,100);//6 20 8 9 5
  12.         l.insert(7,-10);//7 6 20 8 9 5
  13.         l.insert(1,2);//7 6 1 20 8 9 5
  14.         l.push_back(10).push_back(15).travel();
  15.         l.erase(0);//x7: 6 1 20 8 9 10 15
  16.         l.erase(l.size()-1);//x15: 6 1 20 8 9 10
  17.         l.erase(2);//x20: 6 1 8 9 10
  18.         l.travel();
  19.         l.push_back(6);//6 1 8 9 10 6
  20.         l.insert(6,3);//6 1 8 6 9 5 10 6
  21.         l.travel();
  22.         l.remove(6);//1 8 9 5 10
  23.         l.travel();
  24.         l.set(0,666);
  25.         l.set(4,789);
  26.         l.set(l.find(9),123);
  27.         l.set(1,777);
  28.         l.travel();
  29.         cout << l.front() << "..." << l.back() <<',' << l.size() << "个" <<  endl;
  30.         while(!l.empty())l.erase(0);
  31.         cout <<"size:" << l.size() << endl;
  32.         return 0;
  33. }
复制代码
makefile
  1. .PHONY:clean
  2. CC=g++
  3. CPPFLAGS=-Wall -g
  4. BIN= main
  5. OBJS= list.cpp main.cpp
  6. $(BIN):$(OBJS)
  7.         $(CC) $(CPPFLAGS) $^ -o $@
  8. %.o:%.cpp
  9.         $(CC) $(CPPFLAGS) -c $< -o $@
  10. clean:
  11.         rm -f *.o $(BIN)
复制代码

论坛徽章:
324
射手座
日期:2013-08-23 12:04:38射手座
日期:2013-08-23 16:18:12未羊
日期:2013-08-30 14:33:15水瓶座
日期:2013-09-02 16:44:31摩羯座
日期:2013-09-25 09:33:52双子座
日期:2013-09-26 12:21:10金牛座
日期:2013-10-14 09:08:49申猴
日期:2013-10-16 13:09:43子鼠
日期:2013-10-17 23:23:19射手座
日期:2013-10-18 13:00:27金牛座
日期:2013-10-18 15:47:57午马
日期:2013-10-18 21:43:38
3 [报告]
发表于 2014-06-10 11:15 |只看该作者
模板不能分离吧,把cpp中的代码都放到.h中

论坛徽章:
0
4 [报告]
发表于 2014-06-10 11:20 |只看该作者
回复 3# hellioncu


    嗯,我试试,可以了,谢谢
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP