Quick answer: C2027 means the compiler knows the type exists but has only seen a forward declaration such as class UCameraComponent;, and the line needs the full class. Include the header that defines the type in the .cpp file where the error is reported, for example #include "Camera/CameraComponent.h". Keep the forward declaration in your header; it is correct there. If the include then fails or the linker complains, the header’s module is missing from your Build.cs.
This error appears the first time you do something with a component or actor that your header only mentions by pointer: call a function on it, create it, cast to it. The header compiles, the .cpp does not, and the message points at a type you are sure exists. The cause is always the same, and the fix is one include line once you know which one. This guide covers what a forward declaration allows, the related messages MSVC and Clang print for the same mistake, how to find the right header and module, and what to do when two headers include each other.
- The exact error
- Declared is not the same as defined
- What works with a forward declaration and what does not
- The fix: forward declare in the header, include in the .cpp
- Finding the right header
- When the header itself needs the include
- When the header is in another module
- Circular includes
- Why it compiled before
- Where Bugnet fits, and where it does not
- Related guides
- Frequently asked questions
The exact error
D:\Projects\MyGame\Source\MyGame\HeroCharacter.cpp(18): error C2027: use of undefined type 'UCameraComponent'
D:\Projects\MyGame\Source\MyGame\HeroCharacter.h(9): note: see declaration of 'UCameraComponent'
The note is the useful half. It points at the only declaration the compiler has seen, and when you open that line you find a forward declaration, not a class body. It may be in your own header or in an engine header such as GameFramework/Character.h, which forward declares UCharacterMovementComponent, UCapsuleComponent and USkeletalMeshComponent.
The same mistake produces several other messages, depending on what the line does:
| Message | What the line did |
|---|---|
error C2027: use of undefined type 'UCameraComponent', often followed by C2227 on the same line | Accessed a member with -> or :: |
error C2079: 'AHeroCharacter::Item' uses undefined struct 'FItemData' | Declared a member or variable of that type by value |
error C2440: '=': cannot convert from 'AEnemy *' to 'AActor *', with a note that the types pointed to are unrelated | Converted a pointer to a base class pointer. Without the class body the compiler cannot know that AEnemy derives from AActor. As a function argument the same thing is C2664 |
error C2338: static_assert failed: 'Attempting to cast between incomplete types', reported in Casts.h, alone or next to C2027 | Used Cast<T> or CastChecked<T> with a forward declared type. The message is the engine’s own check |
Clang: error: member access into incomplete type 'UCameraComponent' with note: forward declaration of 'UCameraComponent' | The Mac, Linux and Android wording for C2027 |
Clang: error: field has incomplete type 'FItemData' | The Clang wording for C2079 |
IntelliSense: E0393 pointer to incomplete class type "UCameraComponent" is not allowed | The editor’s version, shown before you build. Trust it only if the compiler agrees |
Declared is not the same as defined
C++ has two levels of knowledge about a class. A forward declaration, class UCameraComponent;, says that a class with this name exists. The definition, the full class UCameraComponent : public USceneComponent { ... }; in CameraComponent.h, says how big it is, what it derives from and which members it has. Between the two the type is called incomplete.
A pointer to an incomplete type is fine, because every object pointer has the same size. That is why a header can hold UCameraComponent* Camera; or TObjectPtr<UCameraComponent> Camera; with nothing but the forward declaration. The moment a line needs to know anything about the class itself, the compiler needs the definition, and if the header that contains it is not included in that .cpp, you get C2027.
This is different from error C2065: undeclared identifier, where the compiler has never heard of the name at all. C2027 often appears right after you fix C2065 with a forward declaration: the name is now known, and the next thing the compiler asks for is the body.
What works with a forward declaration and what does not
| Forward declaration is enough | Full definition (the include) is required |
|---|---|
Pointer and reference members: UCameraComponent* Camera; | Member access: Camera->SetupAttachment(...) |
TObjectPtr<T>, TWeakObjectPtr<T>, TSubclassOf<T> and TArray<T*> members | Static access: AEnemy::StaticClass() |
Function declarations that take or return T* or T& | CreateDefaultSubobject<T>, NewObject<T>, SpawnActor<T> |
Comparing a pointer with nullptr or another pointer of the same type | Cast<T> and CastChecked<T>, in either direction |
| Passing the pointer along unchanged to a function that takes the same type | Converting T* to a base class pointer such as AActor* or UObject* |
Inheriting from T | |
A member or local variable of type T by value, and sizeof(T) |
The template functions in the right column explain a confusing variant of the error: the file and line in the message are inside the engine, in Object.h or Casts.h. CreateDefaultSubobject<UCameraComponent> calls UCameraComponent::StaticClass() inside the engine header, so that is where the compiler gives up. Read down to the line that says note: see reference to function template instantiation. It names your file and your line, and that file is the one missing the include.
The fix: forward declare in the header, include in the .cpp
The header is correct as it stands:
// HeroCharacter.h
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Character.h"
#include "HeroCharacter.generated.h"
class UCameraComponent; // forward declaration: enough for a pointer
UCLASS()
class MYGAME_API AHeroCharacter : public ACharacter
{
GENERATED_BODY()
public:
AHeroCharacter();
protected:
UPROPERTY(VisibleAnywhere)
TObjectPtr<UCameraComponent> Camera;
};
Broken source file:
// HeroCharacter.cpp
#include "HeroCharacter.h"
AHeroCharacter::AHeroCharacter()
{
Camera = CreateDefaultSubobject<UCameraComponent>(TEXT("Camera")); // C2027, reported in Object.h
Camera->SetupAttachment(RootComponent); // C2027
GetCharacterMovement()->MaxWalkSpeed = 450.f; // C2027: 'UCharacterMovementComponent'
}
Fixed:
// HeroCharacter.cpp
#include "HeroCharacter.h" // matching header first
#include "Camera/CameraComponent.h" // UCameraComponent
#include "GameFramework/CharacterMovementComponent.h" // UCharacterMovementComponent
AHeroCharacter::AHeroCharacter()
{
Camera = CreateDefaultSubobject<UCameraComponent>(TEXT("Camera"));
Camera->SetupAttachment(RootComponent);
GetCharacterMovement()->MaxWalkSpeed = 450.f;
}
The third line of the constructor is the one that surprises people. Nothing in your code mentions UCharacterMovementComponent by name. GetCharacterMovement() returns a pointer to it, Character.h only forward declares it, and the -> needs the body. The rule to remember: include the header for the type on the left of every ->, even when that type came out of a getter.
Do not fix this by moving the include into the header. It works, and it makes every file that includes HeroCharacter.h compile CameraComponent.h as well, which adds up across a project and is how circular includes start. An include in a header also has to sit above the .generated.h line, or you trade this error for #include found after .generated.h file.
Finding the right header
- Read the type name in the message. Engine class headers are named after the class without its
A,UorFprefix:UCameraComponentis inCameraComponent.h,FTimerManageris inTimerManager.h. - Look the class up in Epic’s C++ API reference. Each class page lists the module, the header path and the include line to copy.
- Or search the engine source for the file name. The include path is the part of the path after the module’s
PublicorClassesfolder:Engine/Source/Runtime/Engine/Classes/Camera/CameraComponent.hbecomes"Camera/CameraComponent.h". - For your own classes, include the header that contains the class body, by its path under your module’s
Publicfolder or module folder.
The includes behind most first encounters with this error:
| Line that fails | Undefined type | Include |
|---|---|---|
GetCharacterMovement()->... | UCharacterMovementComponent | GameFramework/CharacterMovementComponent.h |
GetCapsuleComponent()->... | UCapsuleComponent | Components/CapsuleComponent.h |
GetMesh()->... | USkeletalMeshComponent | Components/SkeletalMeshComponent.h |
PlayerInputComponent->BindAxis(...) | UInputComponent | Components/InputComponent.h |
GetWorld()->SpawnActor<...>(...) | UWorld | Engine/World.h |
GetWorldTimerManager().SetTimer(...) | FTimerManager | TimerManager.h |
DamageEvent.DamageTypeClass in TakeDamage | FDamageEvent | Engine/DamageEvents.h |
SpringArm->TargetArmLength = ... | USpringArmComponent | GameFramework/SpringArmComponent.h |
When the header itself needs the include
A forward declaration is not always enough in the header. Include the real header there, above the .generated.h line, in these cases:
- The base class.
class AHeroCharacter : public ACharacterneedsGameFramework/Character.h. A forward declared base giveserror C2504: 'ACharacter': base class undefined. - Members held by value. A struct such as
FItemData Item;or an enum member needs its definition, because the compiler has to lay out your class. This is C2079. If the member is aUPROPERTY, Unreal Header Tool usually complains first with Unrecognized type. - Function bodies written in the header. An inline getter that does
return Camera->FieldOfView;uses the type like any other code. Move the body to the.cppand the forward declaration is enough again. - The error is reported in a
.gen.cppfile. The generated source for your class includes your header and little else, so a type it needs in full has to be complete in the header.
When the header is in another module
The include line is half of the story for anything outside the modules your project already depends on. Suppose the undefined type is UUserWidget. You add:
#include "Blueprint/UserWidget.h"
and the build now fails with fatal error C1083: Cannot open include file: 'Blueprint/UserWidget.h'. That header belongs to the UMG module, and your module can only see headers of modules listed in its Build.cs:
// Source/MyGame/MyGame.Build.cs
PublicDependencyModuleNames.AddRange(new string[] {
"Core", "CoreUObject", "Engine", "InputCore", "UMG"
});
The module name is on the same API reference page as the include line. Missing the dependency shows up in one of two ways: C1083 when the header cannot be found at all, covered in the C1083 guide, or LNK2019 when the header was found through another route and the linker cannot find the code.
Circular includes
If the include is already there and the type is still undefined, two headers probably include each other:
// HeroCharacter.h
#include "Weapon.h" // needs AWeapon
// Weapon.h
#include "HeroCharacter.h" // needs AHeroCharacter
Whichever header is read first includes the other one, which tries to include the first again. #pragma once skips that second visit, so one of the two classes is compiled before the other has been defined. The symptoms are C2027, C2065 or C2504 in a file that plainly has the include, and they move around depending on which .cpp is being compiled.
The fix is the same pattern as before. A weapon that stores its owner and a character that stores its weapon both hold pointers, so neither header needs the other:
// Weapon.h
class AHeroCharacter; // no include
UPROPERTY()
TObjectPtr<AHeroCharacter> OwningHero;
// Weapon.cpp
#include "Weapon.h"
#include "HeroCharacter.h" // full type, where it is used
When both headers need something from each other by value, usually a struct or an enum, move that type into a third header (for example WeaponTypes.h) that includes neither class, and include it from both.
Why it compiled before
A .cpp with a missing include can build for a long time. Unreal’s unity builds merge source files, so a file can use an include that belongs to a neighbor. A game module also compiles against the engine’s shared precompiled header, which in UE5 already contains Components/StaticMeshComponent.h, Components/SkeletalMeshComponent.h, Components/InputComponent.h, Engine/World.h and TimerManager.h, among many others. That is why calling functions on a UStaticMeshComponent usually works with no include while the same code with a UCameraComponent does not, and why use of undefined type 'UStaticMeshComponent' tends to appear later: after files are added or removed, in a build with precompiled headers turned off, or in a plugin compiled inside another project.
Engine upgrades do the same, because Epic keeps removing includes from engine headers that were only there by accident. FDamageEvent is the usual example: it lives in Engine/DamageEvents.h and used to arrive through Engine/EngineTypes.h. The section on unity builds in the C2065 guide shows how to build once with unity and precompiled headers off so that every missing include is reported in one pass.
To confirm the fix, build Development Editor from the IDE and read the Output window, not the Error List. Compiler errors have codes that start with C; entries that start with E come from IntelliSense and can stay red for a while after a successful build.
Where Bugnet fits, and where it does not
A compile error never reaches Bugnet. C2027 stops the build on your machine, and no player or tester will ever see it. Bugnet has nothing to add until the game runs.
Once it compiles, the same pointers have a runtime failure of their own: the type is complete, and the object is not there. A component that was never created or a cast that returned nullptr crashes on the same -> the compiler was complaining about. Guard the ones that must exist with if (ensure(Camera)) or check(Camera). Bugnet’s Unreal SDK files ensure failures, failed check and verify and fatal errors with the engine’s error text and recent log lines, and reports with the same title stack into one entry with an occurrence count. Ensures and checks are compiled out of Shipping builds by default, so this helps in Development builds; check vs ensure vs verify covers what ships. A crash on a pointer you did not guard is a native crash, and Bugnet does not process minidumps or symbolicate call stacks. See the SDK docs for setup.
Related guides
- How to Fix “error C2065: undeclared identifier” in Unreal Engine
- How to Fix “fatal error C1083: Cannot open include file” in Unreal Engine
- How to Fix “error LNK2019: unresolved external symbol” in Unreal Engine
- How to Fix “#include found after .generated.h file” in Unreal Engine
- How to Fix an Access Violation Crash in Unreal Engine
Frequently asked questions
What does error C2027 use of undefined type mean in Unreal Engine?
The compiler has seen a forward declaration of the type, such as class UCameraComponent, but not the class body, and the line needs to know the class members or size. Include the header that defines the type in the cpp file where the error is reported.
What is the difference between a forward declaration and an include in Unreal C++?
A forward declaration only tells the compiler that a class with that name exists, which is enough to store a pointer or a TObjectPtr to it. An include brings in the full class definition, which is needed to call functions on it, create it, cast to it or inherit from it. The usual pattern is to forward declare in the header and include in the cpp file.
Why is error C2027 reported inside Object.h or Casts.h and not in my file?
Template functions such as CreateDefaultSubobject, NewObject and Cast use the type inside the engine header, so the compiler fails there. The lines below the error include a note that says see reference to function template instantiation, and that note names your file and line. That file is the one missing the include.
How do I find which header to include for an Unreal class?
Engine headers are named after the class without its A, U or F prefix, and the class page in the C++ API reference lists the module and the include line. If the module is not already in your Build.cs dependency list, add it there as well, or the include fails with C1083.
Why do I get C2027 when the header is already included?
Two headers probably include each other. Pragma once skips the second visit, so one class is compiled before the other is defined. Replace the include in at least one of the headers with a forward declaration and include the full header in the cpp file.
Forward declare in the header, include in the .cpp. Every line that uses -> or :: on a type, creates it or casts to it needs the header that defines that type, including types that came out of a getter.