【问题标题】:LNK2005 and LNK1169 while programing signal in QTLNK2005 和 LNK1169 在 QT 中编程信号时
【发布时间】:2021-10-09 17:18:04
【问题描述】:

虽然我一直在 QT 中编写 2 个QThread 之间的连接。第一个是QMainWindow,第二个是我自己的类DB,它基于QThread,因为我不想在从数据库服务器下载数据的过程中停止GUI线程。但是当我将signals 添加到我的新班级时,一切都出错了。我收到了错误:

错误

Error   LNK2005 "public: void __cdecl DB::statusChanged(void)" (?statusChanged@DB@@QEAAXXZ) already defined in mocs_compilation.cpp.obj 
Error   LNK2005 "public: void __cdecl DB::dataDownloaded(void)" (?dataDownloaded@DB@@QEAAXXZ) already defined in mocs_compilation.cpp.obj     
Error   LNK1169 one or more multiply defined symbols found

一分钟前,代码一切正常,但是当我向文件添加信号时,程序停止工作。

代码

数据库.h

#ifndef DB_H
#define DB_H

#include <qthread.h>
class patient;

class DB: public QThread
{
    Q_OBJECT
//--------------------
//CONSTRUCTORS AND DESTRUCTORS
//--------------------
public:
    DB();
    DB(std::vector<patient> *list);
    ~DB();
//--------------------
//SLOTS AND SIGNALS
//--------------------
signals:
    void statusChanged();
    void dataDownloaded();
private:
    std::vector<patient> *listOfPatients;
//--------------------
//METHODS
//--------------------
public:
    void run();
    DB& operator=(const DB& source);
};
#endif  //DB_H

DB.cpp

#include "DB.h"
#include "patient.h"

//--------------------
//CONSTRUCTORS AND DESTRUCTORS
//--------------------
DB::DB()
{
    listOfPatients = nullptr;
}
DB::DB(std::vector<patient> *list)
{
    listOfPatients = list;
}
DB::~DB()
{
}
//--------------------
//SIGNAL AND SLOTS
//--------------------
void statusChanged()
{
}
void dataDownloaded()
{
}
//--------------------
//METHODS
//--------------------
void DB::run()
{
    //import data from DB - to be implement
    emit dataDownloaded();
}

DB& DB::operator=(const DB &source)
{
    this->listOfPatients = source.listOfPatients;
    return *this;
}

你有什么想法吗?

【问题讨论】:

    标签: c++ qt lnk2005


    【解决方案1】:

    问题是我试图定义信号,这是在 QT 中自动定义的。我不知道,但是一条评论促使我检查当我删除 DB.cpp 中的信号定义时发生了什么,这就是问题的线索。

    所以正确的DB.cpp 文件是:

    #include "DB.h"
    #include "patient.h"
    
    //--------------------
    //CONSTRUCTORS AND DESTRUCTORS
    //--------------------
    DB::DB()
    {
        listOfPatients = nullptr;
    }
    DB::DB(std::vector<patient> *list)
    {
        listOfPatients = list;
    }
    DB::~DB()
    {
    }
    //--------------------
    //SIGNAL AND SLOTS
    //--------------------
    
    //NO SIGNAL DEFINITION THERE
    
    //--------------------
    //METHODS
    //--------------------
    void DB::run()
    {
        //import data from DB - to be implement
        emit dataDownloaded();
    }
    
    DB& DB::operator=(const DB &source)
    {
        this->listOfPatients = source.listOfPatients;
        return *this;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-10-14
      • 2019-08-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多