Hello all;
Recently , I write a link list project using c++ template .But it's does work well.
Firstly , I use Inclusion compilation model so I add a sentence "#include "LinkList.cpp"" in head file, but it comes out an error " error: redefinition of 'LinkList<T>::LinkList()' ";
Secondly , I use seperate compilation model so I add a sentence "export template <class T> class LinkList;" in source file . However, there comes an error "error: undefined reference to `LinkList<char>::LinkList()' " too.
Thirdly , I add a sentence "#include "LinkList.cpp"; ". It's worked and the results are easy to understand!
But , how to use Inclusion compilation Model and separate compilation Model in Qt?
Thank for you reply!!!

Qt Code:
  1. //head file LinkList.h
  2. #ifndef LINKLIST_H
  3. #define LINKLIST_H
  4. #include<iostream>
  5.  
  6. template <class T>
  7. struct ListNode
  8. {
  9. T data;
  10. ListNode<T> *next;
  11. };
  12.  
  13. template <class T>
  14. class LinkList
  15. {
  16. public:
  17. LinkList();
  18. virtual ~LinkList();
  19.  
  20. bool GetElem(int loca,T &elem);
  21. bool ListInsert(int loca,T elem);
  22. bool ListDelete(int loca,T &elem);
  23. void ListAppending(T item);
  24. int ListLens();
  25.  
  26. private:
  27. ListNode<T> *p_head;
  28. int i_listlen;
  29. };
  30.  
  31. #endif // LINKLIST_H
  32.  
  33. //source file LinkList.cpp
  34. #include<iostream>
  35. #include "LinkList.h"
  36.  
  37. template <class T>
  38. ...
  39.  
  40. //test file main.cpp
  41. #include <iostream>
  42. #include "LinkList.h"
  43. int main()
  44. {
  45. LinkList<char> c_list;
  46. c_list.ListAppending('a');
  47. c_list.ListAppending('b');
  48. c_list.ListAppending('c');
  49. int lens = c_list.ListLens();
  50. std::cout<<"lens = "<<lens<<std::endl;
  51. return 0;
  52. }
To copy to clipboard, switch view to plain text mode