提问人:CybeX 提问时间:10/14/2019 更新时间:10/14/2019 访问量:601
对已定义的接口析构函数的未定义引用
Undefined reference to already defined interface destructor
问:
我有一个如下界面:QInterruptable
#ifndef QINTERRUPTABLE_H
#define QINTERRUPTABLE_H
class QInterruptable {
public:
virtual void pause();
virtual void resume();
virtual void interrupt();
virtual ~QInterruptable(){
}
};
#endif // QINTERRUPTABLE_H
的示例用法是将 WorkerObject 子类化以获得附加功能。QInterruptable
QThread
QInterruptable
在我的头文件中使用ThreadWorker
#ifndef THREADWORKER_H
#define THREADWORKER_H
#include <QObject>
#include <functional>
#include <QWaitCondition>
#include <QMutex>
#include "QInterruptable.h"
class ThreadWorker : public QObject, public QInterruptable
{
Q_OBJECT
private:
QMutex mutex;
QWaitCondition *waitCondition;
std::function<void ()> runnable;
bool shouldPause = false;
public:
explicit ThreadWorker(QObject *parent = nullptr);
ThreadWorker(std::function<void ()> func);
~ThreadWorker();
void setRunnable(const std::function<void ()> &value);
signals:
void started();
void progress(int value);
void finished();
void error();
public slots:
virtual void run();
virtual void cleanup();
// QInterruptable interface
public:
void pause()
{
shouldPause = true;
}
void resume()
{
shouldPause = false;
}
void interrupt()
{
}
QMutex& getMutex();
QWaitCondition *getWaitCondition() const;
void setWaitCondition(QWaitCondition *value);
bool getShouldPause() const;
#endif // THREADWORKER_H
编译时,我遇到了这个错误:
debug/threadworker.o: In function `ZN14QInterruptableD2Ev':
C:\Users\CybeX\QtProjects\build-MyAwesomeApp-Desktop_Qt_5_13_1_MinGW_32_bit-Debug/../qMyAwesomeApp/3rd-party/QThreading/QThreading/QInterruptable.h:9: undefined reference to `vtable for QInterruptable'
debug/threadworker.o: In function `ZN14QInterruptableC2Ev':
C:\Users\CybeX\QtProjects\build-MyAwesomeApp-Desktop_Qt_5_13_1_MinGW_32_bit-Debug/../qMyAwesomeApp/3rd-party/QThreading/QThreading/QInterruptable.h:4: undefined reference to `vtable for QInterruptable'
collect2.exe: error: ld returned 1 exit status
mingw32-make[1]: *** [Makefile.Debug:195: debug/MyAwesomeApp.exe] Error 1
mingw32-make: *** [Makefile:38: debug] Error 2
mingw32-make[1]: Leaving directory 'C:/Users/CybeX/QtProjects/build-MyAwesomeApp-Desktop_Qt_5_13_1_MinGW_32_bit-Debug'
09:29:08: The process "F:\Qt\Qt5.13.1\Tools\mingw730_32\bin\mingw32-make.exe" exited with code 2.
Error while building/deploying project MyAwesomeApp (kit: Desktop Qt 5.13.1 MinGW 32-bit)
When executing step "Make"
我查看了实现 C++ 接口的解决方案和解决方案。我找到了这个,这个,这个特别提到了我的问题。undefined references to destructors
如您所见,我尝试用
virtual ~QInterruptable(){
}
这会导致上述错误。
如果我使用这样的纯虚拟描述器:
virtual ~QInterruptable() = 0;
然后我把问题传递给子类,即 出现以下错误:ThreadWorker
debug/threadworker.o: In function `ZN12ThreadWorkerC2EP7QObject':
C:\Users\CybeX\QtProjects\build-MyAwesomeApp-Desktop_Qt_5_13_1_MinGW_32_bit-Debug/../qMyAwesomeApp/3rd-party/QThreading/QThreading/threadworker.cpp:28: undefined reference to `QInterruptable::~QInterruptable()'
debug/threadworker.o: In function `ZN12ThreadWorkerC2ESt8functionIFvvEE':
C:\Users\CybeX\QtProjects\build-MyAwesomeApp-Desktop_Qt_5_13_1_MinGW_32_bit-Debug/../qMyAwesomeApp/3rd-party/QThreading/QThreading/threadworker.cpp:33: undefined reference to `QInterruptable::~QInterruptable()'
debug/threadworker.o: In function `ZN12ThreadWorkerD2Ev':
C:\Users\CybeX\QtProjects\build-MyAwesomeApp-Desktop_Qt_5_13_1_MinGW_32_bit-Debug/../qMyAwesomeApp/3rd-party/QThreading/QThreading/threadworker.cpp:37: undefined reference to `QInterruptable::~QInterruptable()'
debug/threadworker.o: In function `ZN14QInterruptableC2Ev':
C:\Users\CybeX\QtProjects\build-MyAwesomeApp-Desktop_Qt_5_13_1_MinGW_32_bit-Debug/../qMyAwesomeApp/3rd-party/QThreading/QThreading/QInterruptable.h:4: undefined reference to `vtable for QInterruptable'
collect2.exe: error: ld returned 1 exit status
mingw32-make[1]: *** [Makefile.Debug:195: debug/MyAwesomeApp.exe] Error 1
mingw32-make: *** [Makefile:38: debug] Error 2
mingw32-make[1]: Leaving directory 'C:/Users/CybeX/QtProjects/build-MyAwesomeApp-Desktop_Qt_5_13_1_MinGW_32_bit-Debug'
09:33:51: The process "F:\Qt\Qt5.13.1\Tools\mingw730_32\bin\mingw32-make.exe" exited with code 2.
Error while building/deploying project MyAwesomeApp (kit: Desktop Qt 5.13.1 MinGW 32-bit)
When executing step "Make"
其中违规行是:ThreadWorker.cpp
[28] ThreadWorker::ThreadWorker(QObject *parent) : QObject(parent)
[29] {
[30] waitCondition = new QWaitCondition;
[31] }
[32]
[33] ThreadWorker::ThreadWorker(std::function<void ()> func): runnable(func) {
[34] waitCondition = new QWaitCondition;
[35] }
[36]
[37] ThreadWorker::~ThreadWorker()
[38] {
[39] if(waitCondition != nullptr){
[40] delete waitCondition;
[41] }
[42] }
如何解决对接口析构函数问题的未定义引用?
答:
2赞
pptaszni
10/14/2019
#1
您具有对基类的 vtable 的未定义引用,因为您没有为基类的所有虚拟(机器人不是纯虚拟)方法提供定义,即:、、。现在,如果您将 3 个方法声明为纯虚拟方法,则不需要对它们进行定义。或者,您可以将它们保留为非纯虚拟,并编写一些默认定义。请注意,析构函数也是一个虚拟方法,也必须定义,但是您可以通过显式告诉编译器为您生成一个来简化它。GCC FAQ 中有一个关于它的条目。pause
resume
interrupt
virtual ~QInterruptable() = default
评论
pause
resume
interrupt
virtual ~QInterruptable() = default;