开发者

How to properly solve the "undefined reference to vtable" error?

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 sp开发者_StackOverflow中文版ecific 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. :)


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.


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.


You need to moc your FGui and Ffoo classes.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜