Results 1 to 4 of 4

Thread: Qt Singleton Class

  1. #1
    Join Date
    Jul 2009
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Qt Singleton Class

    Hello Devs,

    I'm trying to use a singleton class, here's my code (it's really simple):

    dao.h
    Qt Code:
    1. class DAO
    2. {
    3. private:
    4. DAO();
    5. ~DAO();
    6. static DAO* m_dao;
    7. public:
    8. static DAO* getInstance();
    9. ...
    10. }
    To copy to clipboard, switch view to plain text mode 

    dao.cpp
    Qt Code:
    1. #include "dao.h"
    2.  
    3. DAO::DAO() { }
    4.  
    5. DAO::~DAO() { }
    6.  
    7. DAO* DAO::getInstance() {
    8.  
    9. if ( ! m_dao ) { // << This causes a crash
    10. m_dao = new DAO();
    11. }
    12.  
    13. return m_dao;
    14. }
    To copy to clipboard, switch view to plain text mode 

    When trying to build the project, I have this message:

    dap.cpp:9: undefined reference to `DAO::m_dao'
    dao.cpp:10: undefined reference to `DAO::m_dao'
    :-1: error: collect2: ld returned 1 exit status

    Am I missing something?. I'm using Qt 4.6.32 with vista 64, Qt Creator 1.3.1
    Last edited by alexismedina; 22nd June 2010 at 15:53. Reason: spelling corrections

  2. #2
    Join Date
    Mar 2009
    Posts
    72
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows
    Thanks
    7

    Default Re: Qt Singleton Class

    Try this:

    dao.cpp
    Qt Code:
    1. DAO* DAO::m_dao = 0;
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jun 2010
    Posts
    31
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    1
    Thanked 3 Times in 3 Posts

    Default Re: Qt Singleton Class

    I believe your problem comes from the fact that you are trying to test for null using
    ( ! m_dao )
    I was able to compile a very similar case, but I test using
    if(m_dao == NULL)

  4. #4
    Join Date
    Jul 2009
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qt Singleton Class

    Quote Originally Posted by zuck View Post
    Try this:

    dao.cpp
    Qt Code:
    1. DAO* DAO::m_dao = 0;
    To copy to clipboard, switch view to plain text mode 


    This one did the trick! Thanks.

Similar Threads

  1. Singleton Menu!
    By ArlexBee-871RBO in forum Qt Programming
    Replies: 3
    Last Post: 2nd December 2009, 11:32
  2. Object instance in a singleton class dissapears
    By vieraci in forum Qt Programming
    Replies: 2
    Last Post: 10th August 2009, 00:51
  3. Replies: 2
    Last Post: 8th October 2006, 17:49
  4. Singleton pattern - end in recursion
    By probine in forum General Programming
    Replies: 6
    Last Post: 29th March 2006, 14:08
  5. trying to use singleton pattern
    By therealjag in forum Newbie
    Replies: 3
    Last Post: 20th February 2006, 02:20

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.