Fixing "undefined reference to `vtable for ..."

These annoying errors have been haunting me the last couple of days, so I figured I should share the most common reason for their occurrence. That is in my projects at least.

This error is caused because the linker in gcc is unable to find the functions you have defined in your headers in your actual code. So if you have a header which looks like this:

``` {lang="cpp-qt"}

ifndef MESH_H

define MESH_H

class Mesh { public: Mesh(); ~Mesh(); virtual void draw(); };

endif // MESH_H

You must at least have these functions defined in your .cpp file:

``` {lang="cpp-qt"}
#include "mesh.h"

Mesh::Mesh() {

Mesh::~Mesh() {

void Mesh::draw() {

After this, make sure you clean your compile environment to make sure no object files are being misinterpreted by the compiler. If you are using Qt or a project with a Makefile, you could just run these three commands (the first only applies to Qt projects).

{lang="bash"} qmake make clean make

Should you still have trouble, make sure that qmake is actually generating your .moc files for any objects that need them. Sometimes it might even be necessary to empty the build directory completely yourself to make sure there are no files left behind that are not cleaned by make clean.