PDA

View Full Version : Qt Singleton Class



alexismedina
22nd June 2010, 14:50
Hello Devs,

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

dao.h


class DAO
{
private:
DAO();
~DAO();
static DAO* m_dao;
public:
static DAO* getInstance();
...
}


dao.cpp


#include "dao.h"

DAO::DAO() { }

DAO::~DAO() { }

DAO* DAO::getInstance() {

if ( ! m_dao ) { // << This causes a crash
m_dao = new DAO();
}

return m_dao;
}


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

zuck
22nd June 2010, 14:53
Try this:

dao.cpp


DAO* DAO::m_dao = 0;

SneakyPeterson
22nd June 2010, 15:52
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)

alexismedina
22nd June 2010, 16:03
Try this:

dao.cpp


DAO* DAO::m_dao = 0;




This one did the trick! Thanks.