C++: undefined reference to `vtable for X' — virtual function not defined
The linker cannot find the class's virtual table, which means at least one virtual member function was declared but never defined (or the file defining it isn't linked).
undefined reference to `vtable forundefined reference to `typeinfo forvtable for X not definedUndefined symbols: vtable forundefined reference to `typeinfo name for Meaning
The compiler emits the vtable in the file that defines the class's first non-inline virtual function (the "key function"). If that function has no body, or its .cpp is missing from the build, you get undefined reference to vtable for X and often typeinfo for X too.
Common causes
- Virtual function declared but never defined
- The .cpp containing the key function is not part of the target
- Pure virtual function called through a base that has no definition
- Missing virtual destructor body (
virtual ~X();never defined)
⚡ Quick fix
- Define every non-pure virtual function (or
= default) - Add the missing .cpp to the build
- Define the destructor:
virtual ~X() = default;
Detailed fix by platform
C++
- Write
virtual ~Base() = default;in the header - Check that every override has a body or
= 0
CMake
- Re-run CMake after adding files; globbed sources are not detected automatically:
cmake -S . -B build
Code examples
Key function missing
// widget.h
struct Widget {
virtual void draw(); // declared ...
virtual ~Widget() = default;
};
// widget.cpp
#include "widget.h"
void Widget::draw() {} // ... and defined here, so the vtable existsHow to diagnose
- Class — Which class is in the message?
- Definitions — Does each virtual function have a body?
- Build — Is its .cpp compiled and linked?
🔧 Still not fixed?
Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:
Similar errors
- cannot declare variable to be of abstract type C++: error: cannot declare variable 'x' to be of abstract type 'Shape' (pure virtual functions)
- LNK1104 MSVC: fatal error LNK1104: cannot open file 'foo.lib' (or 'app.exe' while it is running)
- multiple definition of C/C++: multiple definition of `x'; first defined here — ld returned 1 exit status
Most viewed in C/C++
- terminate called after throwing an instance of C++: terminate called after throwing an instance of 'std::out_of_range' — what(): vector::_M_range_check
- double free or corruption C/C++: free(): double free detected / double free or corruption (out) — Aborted (core dumped)
- expected ';' before C/C++: error: expected ';' before 'x' — missing semicolon
Other ways to find it
Where this error sits
Origin: Application & API. Errors that are often confused with or caused by this one (click to open):
🧠 Still stuck? Analyze your error
Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.
💬 What worked for others
No tips yet — did something else fix this for you?
Share what worked for you
Tips are checked by an editor before they appear. Links are removed.
Report a correction or suggest an improvement
Last updated 15 Oct 2026