Home c++ Error "undefined reference to` vtable for Counter '"in class with Q_OBJECT

Error “undefined reference to` vtable for Counter ‘”in class with Q_OBJECT





  1. I change the structure of the class (add / remove methods or change their signature);
  2. This class inherits from QObject ;
  3. Inside it uses Q_OBJECT ,

on further compilation, I get an error:

undefined reference to `vtable for ClassName ‘

What is the reason and how to fix it?

Answer 1, authority 100%

Set the checkbox build settings to run qmake on every build

Answer 2

The fact is that the project files, namely:

  • all header files,
  • * .cpp – files with the line below #include & lt; cppfilename.moc & gt;

are passed through the so-called meta-object generator, which generates additional declarations and definitions for each class with Q_OBJECT and places them in a separate * .cpp file. For a number of reasons, the class’s virtual function table also ends up in that file.

However, these support files are not always updated on build after making changes to the source file. Therefore, after each class change, you need to clear the build directory or delete it.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions