C++ 对 vtable 的未定义引用...
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9635172/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
undefined reference to vtable for ...
提问by Dr Deo
I am trying to write an Http proxy that basically works like indianwebproxy
我正在尝试编写一个基本上像indianwebproxy一样工作的 Http 代理
So i fired up qtcreator and but one of my classes is failing to compile with the infamous error : undefined reference to vtable for HttpProxyThreadBrowser
. I can't figure out why its doing this. I read through similar questions on Stackoverflow and apparently the problem is with undefined virtual methods that are not pureBut i have not declared any virtual functions. Here is my class
所以我启动了 qtcreator,但我的一个类无法编译,并出现臭名昭著的错误:undefined reference to vtable for HttpProxyThreadBrowser
。我不明白它为什么这样做。我在 Stackoverflow 上通读了类似的问题,显然问题出在不纯的未定义虚方法上,但我还没有声明任何虚函数。这是我的课
class HttpProxyThreadBrowser : public QThread
{
public:
HttpProxyThreadBrowser(QTcpSocket outgoingSocket,QTcpSocket browserSocket,QObject *parent = 0);
~HttpProxyThreadBrowser(){};
void run();
private:
QTcpSocket outgoingSocket;
QTcpSocket browserSocket;
};
And I define the class here in pastebinso as not to bore you. Unfortunately i cant find out why the vtable is undefined. Please assist.
我在pastebin 中定义了这个类,以免让你感到厌烦。不幸的是,我无法找出 vtable 未定义的原因。请协助。
httpproxythreadbrowser.cpp:5: undefined reference to `vtable for HttpProxyThreadBrowser
collect2: ld returned 1 exit status
采纳答案by alexisdm
You can't copy QTcpSocket
s, so it may cause other cryptic errors if you try to pass them by copy rather than by address.
您不能复制QTcpSocket
s,因此如果您尝试通过复制而不是通过地址传递它们,则可能会导致其他神秘错误。
HttpProxyThreadBrowser(QTcpSocket * outgoingSocket,QTcpSocket * browserSocket,QObject *parent = 0);
private:
QTcpSocket* outgoingSocket;
QTcpSocket* browserSocket;
And completely recompiling your project may help, when you change header files, because qmake generated Makefile can sometimes fail to notice the changes.
当您更改头文件时,完全重新编译您的项目可能会有所帮助,因为 qmake 生成的 Makefile 有时可能无法注意到更改。
回答by Simon Richter
The destructor is implicitly virtual
because a base class has a virtual d'tor.
析构函数是隐式的,virtual
因为基类有一个虚拟的 d'tor。
The GNU compiler emits the vtable along with the first non-inline virtual method ("key method"). As your d'tor is defined inside the class, it is implicitly virtual, and as there are no other virtual methods, you don't have a key method.
GNU 编译器将 vtable 与第一个非内联虚拟方法(“关键方法”)一起发出。由于您的 d'tor 是在类中定义的,因此它是隐式虚拟的,并且由于没有其他虚拟方法,因此您没有关键方法。
There is no use case where a concrete class would have only virtual inline methods, as they can be inlined only into derived classes.
没有具体类只有虚拟内联方法的用例,因为它们只能内联到派生类中。
I'd move the definition of the dtor to the implementation file.
我会将 dtor 的定义移到实现文件中。
I'm not sure whether you need to use moc
here as well, or if QThread
derivatives work without (IIRC you need it only for Qt's cast operators, and for signals/slots).
我不确定您是否也需要在moc
这里使用,或者如果QThread
没有衍生品(IIRC,您只需要它用于 Qt 的转换运算符和信号/插槽)。
回答by Sys
I had also a undefined reference to vtable
error and followed the steps in Undefined reference to vtable... Q_OBJECT macro, that adviced me to run qmake
and... it worked!
我也有一个undefined reference to vtable
错误,并按照Undefined reference to vtable... Q_OBJECT macro 中的步骤操作,建议我运行qmake
...
回答by stativ
This is often caused by not linking the files generated by automoc.
这通常是由于没有链接 automoc 生成的文件造成的。
First, you need to run automoc on the headers where classes using Q_OBJECT are defined, in your case "httpproxythreadbrowser.h". This will generate a "*.moc" file.
首先,您需要在定义使用 Q_OBJECT 的类的标头上运行 automoc,在您的情况下为“httpproxythreadbrowser.h”。这将生成一个“*.moc”文件。
Now there are two common approaches how to continue. Either you can #include the .moc file at the end of your .cpp file with class definition or you can pass it to the compiler as anoher source file.
现在有两种常见的方法如何继续。您可以在带有类定义的 .cpp 文件末尾 #include .moc 文件,也可以将其作为另一个源文件传递给编译器。