Search code examples
c++gccg++

Undefined reference to vtable


When building my C++ program, I'm getting the error message

undefined reference to 'vtable...

What is the cause of this problem? How do I fix it?


It so happens that I'm getting the error for the following code (The class in question is CGameModule.) and I cannot for the life of me understand what the problem is. At first, I thought it was related to forgetting to give a virtual function a body, but as far as I understand, everything is all here. The inheritance chain is a little long, but here is the related source code. I'm not sure what other information I should provide.

Note: The constructor is where this error is happening, it'd seem.

My code:

cgamemodule.h

class CGameModule : public Dasher::CDasherComponent {
 public:
  CGameModule() : CDasherModule() {};
  virtual ~CGameModule();

  virtual void HandleEvent(Dasher::CEvent *pEvent);
};

cgamemodule.cpp

#include "cgamemodule.h"

void CGameModule::HandleEvent(Dasher::CEvent *pEvent) {}

Which inherits from....

namespace Dasher {
  class CEvent;
  class CDasherComponent;
};

class Dasher::CDasherComponent {
 public:
  CDasherComponent() {};
  virtual ~CDasherComponent() {};

  virtual void HandleEvent(Dasher::CEvent * pEvent) {};
};

Solution

  • So, I've figured out the issue and it was a combination of bad logic and not being totally familiar with the automake/autotools world. I was adding the correct files to my Makefile.am template, but I wasn't sure which step in our build process actually created the makefile itself. So, I was compiling with an old makefile that had no idea about my new files whatsoever.

    Thanks for the responses and the link to the GCC FAQ. I will be sure to read that to avoid this problem occurring for a real reason.