Results 1 to 5 of 5

Thread: How to write a template using h and cpp ?

  1. #1
    Join Date
    Sep 2010
    Posts
    654
    Thanks
    56
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default How to write a template using h and cpp ?

    I have the .h and cpp files for a class where I want to write a function with a template typevar.

    Qt Code:
    1. template <class TT_1>
    2. void write(TT_1 value);
    To copy to clipboard, switch view to plain text mode 

    I have problems when I want to write the function at the cpp file. I have not auto-completion info neither tooltip info. And sometimes I have strange compile behaviors. (If I modify some in this template is not compiled, sometimes I have to clean the project ...)

    Any idea ? That is , how can I write the template implemention to let QtCreator and mingw works fine ?
    Thanks

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

    Default Re: How to write a template using h and cpp ?

    Use a language with different rules than C++.
    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
    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: How to write a template using h and cpp ?

    Read this thread, you may find it useful.

  4. #4
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How to write a template using h and cpp ?

    A simple explanation comes from the template definition, a template is not a class or a function... a template is just a "pattern" that the compiler uses to generate code for classes or functions.
    So for the compiler to be able to generate the code, it must see both the template definition and declaration (not just declaration).

    There are some tricks: you can include the .cpp file into the .h file (others say to rename the implementation with a different extension then .cpp) but i don't see the benefits of doing that.

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

    Default Re: How to write a template using h and cpp ?

    File extension is meaningless, it's just for the human's benefit. A general rule of a thumb is that the compiler must read the file to use it. C/C++ compilers don't read unrelated files which compiling some other file.
    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.


Similar Threads

  1. Replies: 2
    Last Post: 2nd November 2010, 05:15
  2. Replies: 4
    Last Post: 25th June 2010, 09:21
  3. using the subdirs Template
    By bhs-ittech in forum Newbie
    Replies: 2
    Last Post: 23rd November 2007, 10:45
  4. using the lib template dll option on *nix
    By jamadagni in forum Qt Programming
    Replies: 3
    Last Post: 19th April 2007, 13:14
  5. how to write a template function in a form..
    By nass in forum General Programming
    Replies: 7
    Last Post: 18th December 2006, 19:36

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
  •  
Qt is a trademark of The Qt Company.