Results 1 to 3 of 3

Thread: Problem with template with static QMap

  1. #1
    Join Date
    Apr 2014
    Location
    Poland
    Posts
    2
    Thanks
    1
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Problem with template with static QMap

    Hi, I have a problem with template in which I use QMap:
    Qt Code:
    1. template
    2. <
    3. class IdentifierType,
    4. class BytesToRead
    5. >
    6. class CommandHandler : public Command
    7. {
    8. public:
    9. explicit CommandHandler();
    10. ~CommandHandler();
    11.  
    12. static bool registerCommand(const IdentifierType& identifier, Command* ptrToObject);
    13. static bool unregisterCommand(const IdentifierType& identifier);
    14. QPair<ResponseType::type, QByteArray> handleMessage(QByteArray* datagramPtr);
    15.  
    16. private:
    17. static QMap<IdentifierType, Command*> _associations;
    18. };
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. template <class IdentifierType, class BytesToRead>
    2. bool
    3. CommandHandler<IdentifierType, BytesToRead>
    4. ::registerCommand(const IdentifierType& identifier, Command* ptrToObject)
    5. {
    6. if(_associations.contains(identifier))
    7. return 0;
    8. else
    9. {
    10. _associations.insert(identifier, ptrToObject);
    11. return 1;
    12. }
    13. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. namespace TransportLayerType {
    2. enum type{
    3. V1 = 1
    4. };
    5. }
    6.  
    7. typedef CommandHandler<TransportLayerType::type, quint8> TransportLayerHandler;
    To copy to clipboard, switch view to plain text mode 
    And when I try to do this:
    Qt Code:
    1. TransportLayerHandler::registerCommand(TransportLayerType::V1, new TransportLayerV1());
    To copy to clipboard, switch view to plain text mode 
    I get:
    undefined reference to `CommandHandler<TransportLayerType::type, unsigned char>::_associations'
    Do you have any ideas?

    Thanks for help, Marcin.

  2. #2
    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: Problem with template with static QMap

    static template data member should be declared too (just like for regular, non-template classes), something like:
    Qt Code:
    1. template<typename T, typename U>
    2. QMap<T, Command*> CommandHandler<T,U>::_associations;
    To copy to clipboard, switch view to plain text mode 
    Put it somewhere after the template definition.
    Last edited by stampede; 9th April 2014 at 17:27. Reason: updated contents

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

    marpasternak (9th April 2014)

  4. #3
    Join Date
    Apr 2014
    Location
    Poland
    Posts
    2
    Thanks
    1
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Problem with template with static QMap

    Wow, it works! Thanks a lot!

Similar Threads

  1. How to Initialise Static QMap?
    By ChrisW67 in forum Newbie
    Replies: 4
    Last Post: 20th June 2013, 12:13
  2. Static Init of a QMap
    By Scorp1us in forum Qt Programming
    Replies: 2
    Last Post: 22nd June 2010, 15:08
  3. QMap and static problem
    By longtrue in forum Qt Programming
    Replies: 1
    Last Post: 11th March 2008, 23:14
  4. Qmap problem on a static lib
    By patrik08 in forum Qt Programming
    Replies: 1
    Last Post: 30th March 2007, 12:11
  5. Replies: 4
    Last Post: 14th February 2006, 21:35

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.