c++ - "undefined reference to"单例

标签 c++ singleton

使用以下代码:

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();
//  }

...

为什么我有一个

undefined reference to 'DBConnection::getSingleton()'
collect2:ld returned 1 exit status 

main()DAOPersistenceFactory::getDAOFactory(DAOPersistenceFactory::MySQL); 中不是,而它似乎具有相同的实现?

最佳答案

我感觉您忘记了将 DBConnection.cpp 添加到您的 .pro 文件中。 如果你这样做了,请尝试重新运行 qmake。同时尝试 make clean

过去,我遇到过很多关于过时对象文件的奇怪问题。 ;)

关于c++ - "undefined reference to"单例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4224008/

相关文章:

c++ - 套接字和文件推荐的缓冲区大小

c++ - boost::filesystem 添加引号?

c++ - 使用类模板需要参数列表

c++ - 将数据从字节数组(带零)传递到 istringstream(stringstream)

java - 这个nullPointerException的原因是什么?

c++ - 带有 C++ 方法的 gcc 属性

ios - Swift Singleton Init 在 XCTest 中被调用两次

java - 使用 Singleton 类中的静态 GSON 对象将 Java 对象序列化为 JSON

java - 管理全局对象或列表

unit-testing - 作为接口(interface)访问的单例的单元测试