PDA

View Full Version : Unresolved externals



MarkoSan
17th March 2007, 10:42
Hello again!

Once I want to compile I get following errors:

File cdatabaseconnection.h:



#ifndef CDATABASECONNECTION_H
#define CDATABASECONNECTION_H

// qt headers
#include <QObject>
#include <QSqlDatabase>

// c++ stl headers
#include <memory>

class CDatabaseConnection : public QSqlDatabase
{
//Q_OBJECT

public:
//CDatabaseConnection(QObject *parent);
static std::auto_ptr<CDatabaseConnection>& Instance(QObject *parent);
~CDatabaseConnection();

private:
static std::auto_ptr<CDatabaseConnection> m_pInstance;
};

//typedef std::auto_ptr<CDatabaseConnection> pdbType;

#endif // CDATABASECONNECTION_H


File cdatabaseconnection.cpp:



#include "cdatabaseconnection.h"

/*
CDatabaseConnection::CDatabaseConnection(QObject *parent)
: QSqlDatabase()
{
}
*/

CDatabaseConnection::~CDatabaseConnection()
{
}

std::auto_ptr<CDatabaseConnection>& CDatabaseConnection::Instance(QObject *parent)
{
return m_pInstance;
}


After compilation, I get linker errors:



Error 1 error LNK2001: unresolved external symbol "private: static class std::auto_ptr<class CDatabaseConnection> CDatabaseConnection::m_pInstance" (?m_pInstance@CDatabaseConnection@@0V?$auto_ptr@VC DatabaseConnection@@@std@@A) cdatabaseconnection.obj
Error 2 fatal error LNK1120: 1 unresolved externals C:\Documents and Settings\markofr.NESRAMNIK\Desktop\QBlagajna\Debug \QKlient.exe


Can someone help me please?

jpn
17th March 2007, 10:51
Add

std::auto_ptr<CDatabaseConnection> CDatabaseConnection::m_pInstance = 0;
in cdatabaseconnection.cpp.

MarkoSan
17th March 2007, 11:36
Add

std::auto_ptr<CDatabaseConnection> CDatabaseConnection::m_pInstance = 0;in cdatabaseconnection.cpp.

Where should I put it, can I put it in main.cpp so I can initialize database at app beginning?

jpn
17th March 2007, 14:53
Where should I put it, can I put it in main.cpp so I can initialize database at app beginning?
An initialization of a static member is needed outside the class, in the global scope. I would strongly advise to put the definition (initialization) into cdatabaseconnection.cpp. That's where it belongs, doesn't it? :) You can access the pointer and initialize the actual connection anywhere you want by just including the header.