C++: error: invalid use of incomplete type 'class X' — forward declaration only
You used a class that has only been forward-declared (so its size and members are unknown) where the full definition is required.
invalid use of incomplete typeincomplete type is not allowedforward declaration of 'classmember access into incomplete typeallocation of incomplete type Meaning
A forward declaration (class X;) is enough for pointers and references, but not for creating objects, calling members, sizeof, delete, or deriving. Include the real header in the .cpp that needs it, and keep headers light by forward-declaring only what they truly need.
Common causes
- Accessing members through a pointer when only
class X;is visible - Circular includes (a.h includes b.h which includes a.h)
std::unique_ptr<X>member with X incomplete at the destructor- Using
sizeof(X)ornew Xbefore the definition - Missing
#include <vector>/<string>leaves std types incomplete
⚡ Quick fix
#include "x.h"in the .cpp that uses the members- Break include cycles with forward declarations in headers
- Define the destructor in the .cpp for pimpl classes
Detailed fix by platform
C++
- In headers use
class X;and pointers/references; in the .cpp include the full header - For
std::unique_ptr<Impl>declare~Widget();in the header and defineWidget::~Widget() = default;in the .cpp
Code examples
Forward declaration is not enough to call members
// a.h
class B; // forward declaration
struct A { B* b; void use(); };
// a.cpp
#include "a.h"
#include "b.h" // <-- required here
void A::use() { b->run(); } // error without b.h: invalid use of incomplete type 'class B'How to diagnose
- Declaration — Is the type only forward-declared where it is used?
- Cycle — Do the headers include each other?
- Needs — Do you need the size or members here?
🔧 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)
- discards qualifiers C++: error: passing 'const X' as 'this' argument discards qualifiers [-fpermissive]
- does not name a type C++: error: 'string' does not name a type
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)
- undefined reference to C/C++: undefined reference to `function' — collect2: error: ld returned 1 exit status
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