Results 1 to 4 of 4

Thread: Inclusion compilation Model and separate compilation Model in Qt?

  1. #1
    Join Date
    Mar 2015
    Location
    ustc
    Posts
    4
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Inclusion compilation Model and separate compilation Model in Qt?

    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 

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Inclusion compilation Model and separate compilation Model in Qt?

    Why are you including a cpp file? That's almost never a good idea.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Mar 2015
    Location
    ustc
    Posts
    4
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Inclusion compilation Model and separate compilation Model in Qt?

    Because I saw a blog which tells some tips to fix this error by adding source code into head file , so try including the cpp file in main.cpp and indeed it's work well. However , how can I use Inclusion compilation Model and separate compilation Model in Qt?
    thank you!

  4. #4
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Inclusion compilation Model and separate compilation Model in Qt?


Similar Threads

  1. Replies: 4
    Last Post: 21st October 2013, 21:24
  2. Replies: 24
    Last Post: 18th September 2013, 06:35
  3. Replies: 1
    Last Post: 29th August 2013, 05:41
  4. Replies: 9
    Last Post: 14th February 2013, 19:39
  5. Replies: 0
    Last Post: 2nd November 2011, 11:13

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.