Results 1 to 5 of 5

Thread: problem with using template classes

  1. #1
    Join Date
    Aug 2008
    Posts
    27
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default problem with using template classes

    hi,

    i am trying to derive new classes from a template base class. I simplified my code as below just to
    show the problem.

    Qt Code:
    1. template<class T>
    2. class BaseClass
    3. {
    4. public:
    5. BaseClass(T&);
    6. private:
    7. };
    8.  
    9. //.cpp implementation
    10.  
    11. template<class T>
    12. BaseClass<T>::BaseClass<T>(T& iT)
    13. {
    14. }
    To copy to clipboard, switch view to plain text mode 

    and my derived class as:

    Qt Code:
    1. class DerivedClass : public BaseClass<QString>
    2. {
    3. public:
    4. DerivedClass(QString);
    5. ~DerivedClass();
    6. private:
    7. };
    8.  
    9. //.cpp implementation
    10.  
    11. DerivedClass::DerivedClass(QString st)
    12. : BaseClass<QString>(st)
    13. {
    14. }
    15.  
    16. DerivedClass::~DerivedClass()
    17. {
    18. }
    To copy to clipboard, switch view to plain text mode 

    when i try to compile this code it gives link errors as below

    DerivedClass.obj : error LNK2019: unresolved external symbol "public: __thiscall BaseClass<class QString>::BaseClass<class QString>(QString&)" (??0?$BaseClass@VQString@@@@QAE@XZ) referenced in function "public: __thiscall DerivedClass:erivedClass(QString)" (??0DerivedClass@@QAE@XZ)


    it doesn't generate any error when i do not implement any constructor in the base class. and do not call the base constructor, but i want to implement some code in the base class' constructor.
    so, what is the problem with this?

    thanks

  2. #2
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: problem with using template classes

    As far as I know,, In C++ , if you are defining a parametrized constructor, you need to define a default constructor too.
    Thats the problem in your code I guess

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    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: problem with using template classes

    Template classes have to be defined completely in header files, they can't have .cpp files. Move the constructor body to the header 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.


  4. The following user says thank you to wysota for this useful post:

    arbi (1st September 2010)

  5. #4
    Join Date
    Aug 2008
    Posts
    27
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: problem with using template classes

    nope. that is not required in C++. Anyway, i added default constructor to the class but same error generated again.

    may be deriving new class from template classes does not work but i dont know for sure?

  6. #5
    Join Date
    Aug 2008
    Posts
    27
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: problem with using template classes

    Quote Originally Posted by wysota View Post
    Template classes have to be defined completely in header files, they can't have .cpp files. Move the constructor body to the header file.
    thank you, it worked. I learned something new.

Similar Threads

  1. Replies: 4
    Last Post: 25th June 2010, 09:21
  2. Replies: 0
    Last Post: 2nd February 2009, 14:24
  3. Template classes and abstraction of type
    By Raistlin in forum General Programming
    Replies: 15
    Last Post: 1st April 2008, 10:18
  4. signal/slots in template classes
    By killkolor in forum Qt Programming
    Replies: 2
    Last Post: 6th December 2006, 18:28
  5. problem using template
    By mickey in forum General Programming
    Replies: 6
    Last Post: 18th November 2006, 15:57

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.