Results 1 to 5 of 5

Thread: Templates : Help Please

  1. #1
    Join Date
    Jan 2006
    Location
    Kerala
    Posts
    371
    Thanks
    76
    Thanked 37 Times in 32 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Templates : Help Please

    Is there any issues if a template's implementation and declarations are in seperate files. I am using (GCC) 3.4.3 20041212 (Red Hat 3.4.3-9.EL4). In the Attachments below.
    The TemplateTestError.tar.gz has Implemention of The Item::baseFunction() in Item.cpp and The TemplateTestSuccess.tar.gz has Implemention of The Item::baseFunction() in Item.h itself. Please not that the baseFunction() implemention is not an inline.

    Please go through the Attachments;It would be great if some one could point out where i am going wrong. and Give links or tips on this Issue.

    Thanks in Advance
    Attached Files Attached Files
    We can't solve problems by using the same kind of thinking we used when we created them

  2. #2
    Join Date
    Jan 2006
    Location
    New Zealand
    Posts
    10
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Templates : Help Please

    At the bottom of Item.cpp add the line:

    template class Item<int>;


    That tells the compiler to build code for that particular specialisation, otherwise when the compiler hits that file it doesn't know what's needed.

  3. #3
    Join Date
    Jan 2006
    Location
    Kerala
    Posts
    371
    Thanks
    76
    Thanked 37 Times in 32 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Templates : Help Please

    Quote Originally Posted by Weaver
    At the bottom of Item.cpp add the line:

    template class Item<int>;


    That tells the compiler to build code for that particular specialisation, otherwise when the compiler hits that file it doesn't know what's needed.
    Ya that solves the problem but introduces a new one - It's not usable. I can expect the users of the Item class to type the class name in Item.cpp. There must be an elegant solution isn't it?. Moreover what if i plan to move the implemention to a .so/.dll file. Then what will they do .

    If the implementation is kept in the .h The user even need not bother about the declaration - template class Item<int>;. But now they get to see the implementation and may be even change it .

    I want the implementation to be put in the .cpp and leave the .cpp file intact. so that others can't mess things up.
    We can't solve problems by using the same kind of thinking we used when we created them

  4. #4
    Join Date
    Jan 2006
    Location
    New Zealand
    Posts
    10
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Templates : Help Please

    Well you can't stop people reimplementing your functions... they can always take your header and implement a new library =)


    The problem with templates is that they're only generated "on demand". ie the specialisation of the function is only ever created if it's referred to. When the compiler sees Item.cpp it doesn't know what types of baseFunction() to generate. When it sees a reference it it in another cpp file, it can't see where the function is defined to create a specialisation.

    Hopefully this explains it better:
    http://www.parashift.com/c++-faq-lit...html#faq-35.12


    If you don't know ahead of time what types you'll need, then the best you can do is #include "Item.cpp" at the bottom of Item.h

  5. #5
    Join Date
    Jan 2006
    Location
    Kerala
    Posts
    371
    Thanks
    76
    Thanked 37 Times in 32 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Templates : Help Please

    Thanks That link solved all my doubts
    We can't solve problems by using the same kind of thinking we used when we created them

Similar Threads

  1. Templates Confusion
    By baray98 in forum General Programming
    Replies: 6
    Last Post: 23rd November 2008, 12:14
  2. templates / Q_OBJECT question
    By _borker_ in forum Qt Programming
    Replies: 6
    Last Post: 19th December 2007, 21:35
  3. dynamic_cast and templates
    By KShots in forum General Programming
    Replies: 7
    Last Post: 7th August 2007, 21:01
  4. templates and Q_OBJECT
    By TheKedge in forum General Programming
    Replies: 12
    Last Post: 15th September 2006, 06:41
  5. Templates vcapp and subdirs together
    By xgoan in forum Newbie
    Replies: 1
    Last Post: 24th August 2006, 11:46

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.