2010-11-19 13 views
1

有する "に未定義の参照":次のコードでシングルトン

class DBConnection 
{ 
// Methodes : private 
    private: 
     // Constructeur 
     DBConnection(); 

     // Destructeur 
     ~DBConnection(); 

// Methodes : public 
    public: 
     bool open(); 
     bool close(); 

// Methodes : public : static 
    public: 
     static DBConnection * getSingleton(); 

// Variables 
    private: 
     static DBConnection * singleton; 

     QSqlDatabase conn; 

     QString   driver, 
         host, 
         userName, 
         password, 
         DBName; 
}; 
#endif // DBCONNECTION_HPP 

#include "DBConnection.hpp" 

// Initialisation du singleton dans l'espace global, car static 
    DBConnection * DBConnection::singleton = 0; 

// Methodes : private 
    DBConnection::DBConnection() { 
     this->conn = QSqlDatabase::addDatabase("QMYSQL"); 
      this->conn.setHostName(""); 
      this->conn.setUserName(""); 
      this->conn.setPassword(""); 
      this->conn.setDatabaseName(""); 
    } 

    DBConnection::~DBConnection(){}; 

// Methodes : public 
    bool DBConnection::open() { 
     bool rep = this->conn.isOpen()?true:this->conn.open(); 

     if(!rep) 
      QMessageBox::critical(0, "Erreur critique !", "Impossible d'ouvrir la base de données !"); 

     return rep; 
    } 

DBConnection * DBConnection::getSingleton() { 
     if(singleton == 0) 
      singleton = new DBConnection; 
     return singleton; 
    } 

#ifndef DAOMYSQLFACTORY_HPP 
#define DAOMYSQLFACTORY_HPP 

#include "InterfaceDAOFactory.hpp" 
#include "DAO.hpp" 

class DAOMySQLFactory : public InterfaceDAOFactory 
{ 
// Methodes : private 
    private: 
     // Constructeur 
     DAOMySQLFactory(); 

     // Destructeur 
     ~DAOMySQLFactory(); 
// Methodes : public : heritées 
    public: 
     DAO * getDAOClient(); 

     DAO * getDAOSite(); 

     DAO * getDAOMachine(); 

// Methode : static 
    public: 
     static DAOMySQLFactory * getSingleton(); 

// Variables 
    private: 
     // Instance unique 
     static DAOMySQLFactory * singletonMySQLFactory; 
}; 

#endif // DAOMYSQLFACTORY_HPP 

#include "DAOMySQLFactory.hpp" 
#include "DBConnection.hpp" 
#include "DAOMySQLClient.hpp" 


DAOMySQLFactory * DAOMySQLFactory::singletonMySQLFactory = 0; 

// Methodes : private 
    // Constructeur 
    DAOMySQLFactory::DAOMySQLFactory() {} 
    // Destructeur 
    DAOMySQLFactory::~DAOMySQLFactory() {} 

// Methode : static 
    DAOMySQLFactory * DAOMySQLFactory::getSingleton() { 
     if(singletonMySQLFactory == 0) 
      singletonMySQLFactory = new DAOMySQLFactory; 
     return singletonMySQLFactory; 
    } 

// Methodes : public : heritee 

    DAO * DAOMySQLFactory::getDAOClient() { 
     return 0; 
    } 
... 

#include <QApplication> 

#include "WinMain.h" 

//TEST 
#include "DAOPersistenceFactory.hpp" 
#include "DAO.hpp" 
#include "DAOMySQLFactory.hpp" 
#include "DBConnection.hpp" 

int main(int argc, char *argv[]) 
{ 
    QApplication app(argc, argv); 

//TEST 
    InterfaceDAOFactory * idao = DAOPersistenceFactory::getDAOFactory(DAOPersistenceFactory::MySQL); 
    DAO * d = idao->getDAOClient(); 
    DBConnection::getSingleton(); 


    WinMain fen; 
    fen.show(); 

    return app.exec(); 
} 

#ifndef DAO_HPP 
#define DAO_HPP 

#include <QString> 
#include <QStringList> 
#include <QSqlQuery> 

class DAO { 
// Methodes : public 
    public: 
     DAO(); 
     virtual ~DAO(); 

// Methodes : public : abstraites 
    public: 
     virtual QStringList findAll() = 0; 

// Variable 
    protected: 
     QSqlQuery allQuery; 
}; 

#endif // DAO_HPP 

#include "DAO.hpp" 

DAO::DAO() {} 

DAO::~DAO(){} 

#ifndef DAOMYSQLCLIENT_HPP 
#define DAOMYSQLCLIENT_HPP 

#include <QString> 
#include <QStringList> 
#include <QSqlQuery> 

#include "DAO.hpp" 
#include "DBConnection.hpp" 

class DAOMySQLClient : public DAO 
{ 

// Methodes : public 
    public: 
     DAOMySQLClient(); 
    // DAOMySQLClient(DBConnection * connection); 
//Variables 
    private: 
     DBConnection * conn; 

     QSqlQuery  byIdQuery, 
         byNameQuery; 
}; 

#endif // DAOMYSQLCLIENT_HPP 

#include <QMessageBox> 
#include <QSqlError> 
#include <QVariant> 

#include "DAOMySQLClient.hpp" 


// Methodes : public 
    // Constructeur 
    DAOMySQLClient::DAOMySQLClient() {} 

    // Constructeur 
// DAOMySQLClient::DAOMySQLClient(DBConnection * connection) { 
//  this->conn = connection; 
//  this->conn->open(); 
//  initQueries(); 
// } 

... 

私は

を持っているのはなぜ3210
undefined reference to 'DBConnection::getSingleton()' 
collect2:ld returned 1 exit status 

main()DAOPersistenceFactory::getDAOFactory(DAOPersistenceFactory::MySQL);と同じ実装ではないようですが、

+1

リンクステップを実行するために使用しているgccコマンドラインを表示できますか? –

+0

ファイル全体を置くことはできますか? begin-fileガードがありません。 – Klaim

+0

@ Klaim Editied – canardman

答えて

1

.proファイルにDBConnection.cppを追加するのを忘れてしまったような気がします。 これを実行した場合は、qmakeを再実行してみてください。 make cleanにもお試しください。

私は過去にオブジェクトファイルが古くて奇妙な問題がたくさんありました。 ;)

+0

OK!あなたはそうです:)しかし、それは奇妙です。私はQt Creatorを通してqmakeを実行したときに.proを変更しなかったので、自分自身を.proに変更しなければなりませんでした。 – canardman

+0

時々、奇妙なことが起こります。 99%の "定義されていない参照"エラーは、通常、2つのソースから来ていることを覚えておいてください。 2).cpp/objectファイルがありません。私は助けになることができてうれしいです。 :) – BastiBen

+0

ええ、私はメモを取っています;)、あなたと他のおかげです。 – canardman

関連する問題