如何正确解决“未定义的vtable引用”问题错误?
我已经阅读了常见问题解答和其他几个有关此问题的网站,但我似乎找不到正确的解决方案。我很确定您以前听说过这个问题,如果可能的话,我想请求有关如何解决这个问题的具体帮助。
具体错误是: 错误:未定义对“FGui vtable”的引用 它总是指向 fgui.cpp 中的构造函数,但我没有看到它有任何问题。
FGui是一个继承自名为“FFoo”的类的类,该类又继承自QMainWindow。 相关代码:(
在ffoo.h中:)
class Ffoo : public QMainWindow
{
Q_OBJECT
public:
Ffoo();
~Ffoo();
(...)
};
(在ffoo.cpp中:)
Ffoo::Ffoo()
{
textEdit = 0;
tcpSock = 0;
setupConnectBox();
}
Ffoo::~Ffoo()
{}
FGui文件仍然非常简单,因为我最近才开始制作该类。
(fgui.h:)
class FGui : public Ffoo
{
Q_OBJECT
public:
FGui();
~FGui();
};
(fgui.cpp:)
FGui::FGui() : Ffoo()
{}
FGui::~FGui()
{}
如果有人能告诉我该怎么解决这个问题,我将非常感激。提前致谢。 :)
I've read the FAQ and several other websites about this, but I can't seem to find the proper solution. I'm quite sure you've heard the question before, and I'd like to ask for specific help as to how I should solve this, if possible.
The specific error is:
error: undefined reference to `vtable for FGui'
It always points to the constructor in fgui.cpp, but I don't see anything that could be wrong with it.
FGui is a class inherited from a class named "FFoo", which is inherited from QMainWindow.
Relevant code:
(In ffoo.h:)
class Ffoo : public QMainWindow
{
Q_OBJECT
public:
Ffoo();
~Ffoo();
(...)
};
(In ffoo.cpp:)
Ffoo::Ffoo()
{
textEdit = 0;
tcpSock = 0;
setupConnectBox();
}
Ffoo::~Ffoo()
{}
The FGui files are still very plain, since I only started making the class recently.
(fgui.h:)
class FGui : public Ffoo
{
Q_OBJECT
public:
FGui();
~FGui();
};
(fgui.cpp:)
FGui::FGui() : Ffoo()
{}
FGui::~FGui()
{}
If anybody can tell me what to do to solve this, I would be very grateful. Thanks in advance. :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
尝试重新运行 qmake。如果您使用 Qt Creator,请清理项目(构建/清理全部),然后选择构建/运行 qmake。这在这种情况下通常会有所帮助。
Try to re-run qmake. If you use Qt Creator, clean project (Build/Clean All) and after that choose Build/Run qmake. This often helps in such situations.
您需要针对源文件运行 moc。如果您使用 qmake 创建 makefile,只要您在 .pro 文件中包含 .cpp 和 .h 文件,这种情况就会自动发生。您可能忘记了这些步骤之一。请注意,如果您的类在上次运行 qmake 期间不包含 Q_OBJECT 宏,则仅运行 make 不会调用 moc 来运行。您需要再次运行 qmake 才能执行此操作!
接下来会发生以下过程:如果运行“make”,不仅会编译您的 .cpp 文件,还会由 moc(元对象编译器)创建一个附加的 .cpp 文件,然后也进行编译。第二个 .cpp 文件包含信号的实现。请注意,信号实际上是普通方法,它将调用“转发”到连接的槽(或信号)。这个实现是 moc 生成的(除其他外)。即使您的类不包含信号,由于一些内部使用的(?)虚函数,类也需要有一个 vtable...但是,如果您的源文件包含基于 QObject 的类,则确实需要运行 moc。
You need to run moc against your source files. This happens automatically if you created your makefile using qmake as long as you included the .cpp and .h files in your .pro file. You may have forgotten one of these steps. Please note, that if your class didn't contain the Q_OBJECT macro during your last run of qmake, simply running make doesn't invoke moc to run. You need to run qmake again to do so!
What happens then is the following process: If you run "make", not only your .cpp file gets compiled, but also an additional .cpp file gets created by the moc (meta object compiler) and then gets compiled too. This second .cpp file contains the implementations of the signals. Note that signals are acutally ordinary methods which "forward" the call to the connected slots (or signals). This implementation is what the moc generates (among other things). Even if your classes don't contain signals, the classes need to have a vtable due to some internally used (?) virtual functions... However, moc really needs to be run if your source file contains a QObject-based class.
您需要 moc 您的 FGui 和 Ffoo 类。
You need to moc your FGui and Ffoo classes.
对 vtable 的未定义引用意味着您没有实现纯虚拟方法。它可能是 Ffoo 中的纯虚拟方法,您忘记在 FGui 中实现,或者,如果您正在使用信号,则可能您没有使用元对象编译器(它将实现缺少的方法),正如其他人所说。
Undefined reference to vtable means that you didn't implemented a pure virtual method. It may be a pure virtual method in Ffoo that you forgot to implement in FGui or, if you are using signals, maybe you are not using the Meta-Object Compiler (which would implement the missing methods), as others have stated.
当您在源文件开发后期添加 Q_OBJECT 宏时,通常会遇到这种情况。 Makefile 尚未认为您必须 moc 该文件,因此它不需要,并且您会收到这些 vtable 错误。正如其他人所指出的,只需重新运行 qmake 即可。如果您使用 Creator,它位于“构建”菜单下。
You usually get this when you add a Q_OBJECT macro late in the development of a source file. The Makefile doesn't yet think you have to moc the file, so it doesn't, and you get these vtable errors. As others have indicated, just re-run qmake. If you're using Creator, it's under the Build menu.