본문 바로가기
Unreal Engine 5

언리얼 엔진 - 전방 선언하고 include 안 할 때를 주의하자(error C2440: 'static_cast' : cannot convert from ...)

by 니키티스 2026. 5. 31.

전방 선언과 include

...\Object.h(151): error C2440: 'static_cast': 
cannot convert from 'UObject *' to 'UTargetClass'
  • 멤버에 대해 클래스 전방 선언(Forward declaration)을 하게 되면, 나중에 Include하지 않은 상태로 할당하게 되면 오류가 생길 수 있다.
  • 멤버에 접근하거나, CreateDefaultSubobject 등으로  무조건 해당 클래스를 include해놔야 한다.
  • TObjectPtr<class UTargetClass> TargetComponent; 
  • 헤더에서는 위와 같이 선언해놓고, 실제로 생성할 때 아래와 같이 해당 클래스의 헤더를 Include하지 않으면 클래스 타입을 알지 못해 문제가 생길 수 있다.
// 헤더 포함 안 함

AOBCharacterEnemy::AOBCharacterEnemy()
{
	TargetComponent = CreateDefaultSubobject<UTargetClass>(TEXT("TargetableComponent"));
}

이 경우 제깍제깍 Include를 잘 해놓자.

#include ".../TargetClass"

댓글