# C++: error: invalid use of incomplete type 'class X' — forward declaration only

**Code:** `incomplete type` · **Category:** C/C++ · **Seen on:** C/C++, GCC, Clang

You used a class that has only been forward-declared (so its size and members are unknown) where the full definition is required.

## 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)` or `new X` before the definition
- Missing `#include <vector>` / `<string>` leaves std types incomplete

## Quick fix

1. `#include "x.h"` in the .cpp that uses the members
2. Break include cycles with forward declarations in headers
3. Define the destructor in the .cpp for pimpl classes

## Detailed fix by platform

### C++

1. In headers use `class X;` and pointers/references; in the .cpp include the full header
2. For `std::unique_ptr<Impl>` declare `~Widget();` in the header and define `Widget::~Widget() = default;` in the .cpp

## Code examples

### Forward declaration is not enough to call members

```cpp
// 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

1. **Declaration** — Is the type only forward-declared where it is used?
2. **Cycle** — Do the headers include each other?
3. **Needs** — Do you need the size or members here?

---
Source: [WhatIsMyError](https://whatismyerror.com/errors/cpp/incomplete-type)
