Results 1 to 3 of 3

Thread: Template of ingredient class

  1. #1
    Join Date
    Mar 2009
    Posts
    29
    Thanks
    14
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Template of ingredient class

    I want create class with pattern of function. When I add definition of function to *.cpp file and call it in mainwindow.cpp, I have bugs:
    Qt Code:
    1. mainwindow.o:-1: error: In function `ZN10MainWindowC2EP7QWidget':
    2. mainwindow.cpp:6: undefined reference to `readmemory::readmemory()'
    3. mainwindow.o:-1: error: In function `ZN10MainWindowC1EP7QWidget':
    4. mainwindow.cpp:6: undefined reference to `readmemory::readmemory()'
    5. mainwindow.o:-1: error: In function `ZN10MainWindow11startSearchEv':
    6. mainwindow.cpp:46: undefined reference to `void readmemory::test<int>(int)'
    7. :-1: error: collect2: ld returned 1 exit status
    To copy to clipboard, switch view to plain text mode 

    It's my exemplary code:
    class.h
    Qt Code:
    1. class Pattern{
    2. template<typename T> void function(T);
    3. };
    To copy to clipboard, switch view to plain text mode 

    class.cpp
    Qt Code:
    1. template<typename T>
    2. void Pattern::function(T a)
    3. {
    4. //code
    5. }
    To copy to clipboard, switch view to plain text mode 

    mainwindow.h
    Qt Code:
    1. Pattern patternClass;
    2. int a;
    3. patternClass.function(a);
    To copy to clipboard, switch view to plain text mode 
    I tried use "export" like in Visual Studio and include class.cpp to class.h, but it doesn't work. Is it possible to use template like in my test code?
    Last edited by Trok; 21st August 2009 at 11:05.

  2. #2
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Template of ingredient class

    You declare a template... this makes the compiler happy.
    But at link time, the calls can't be resolved as those functions have not been implemented (only called):
    You have to tell the compiler that a specialization for int has to be compiled.

    possible way to do it:
    add
    Qt Code:
    1. template<> void Pattern::function(int a)
    2. {
    3. //code
    4. }
    To copy to clipboard, switch view to plain text mode 
    to class.cpp

    or just leave the implementation in class.hpp and save you the trouble.
    (putting specializations into the .cpp will reduce code bloat, though.)

  3. The following user says thank you to caduel for this useful post:

    Trok (21st August 2009)

  4. #3
    Join Date
    Mar 2009
    Posts
    29
    Thanks
    14
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Template of ingredient class

    So, when i want use my function like:
    Qt Code:
    1. double a;
    2. patternClass.function(a);
    To copy to clipboard, switch view to plain text mode 
    Must I define new function with argument double? If it's true, I can glut function with the same result.

Similar Threads

  1. Extending two class "the same way"
    By caduel in forum General Programming
    Replies: 3
    Last Post: 22nd July 2009, 22:55
  2. class in the class
    By baray98 in forum General Programming
    Replies: 2
    Last Post: 23rd July 2008, 07:01
  3. class QHBoxLayout
    By csvivek in forum Installation and Deployment
    Replies: 2
    Last Post: 10th April 2008, 07:57
  4. How to use Signal through direct connection
    By santosh.kumar in forum Qt Programming
    Replies: 1
    Last Post: 14th December 2007, 07:07
  5. Replies: 2
    Last Post: 4th May 2006, 19:17

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.