C2440: Cannot Convert from ‘type1’ to ‘type2’ in UE5 C++ – What It Means & How to Fix It
🧠 Why You’re Seeing This Error
The C2440
error means you’re trying to assign or initialize a variable with a value of the wrong type, and C++ doesn’t know how to convert between them.
In Unreal Engine 5, this commonly happens when:
- You assign a pointer to a non-pointer (or vice versa)
- You try to assign
nullptr
orNULL
to an object (not a pointer) - You mix up
FString
andconst char*
orstd::string
- You attempt an invalid cast between class types or structures
C++ is strict about type conversions — and UE5 adds its own types (FString
, FVector
, AActor*
, etc.), which don’t auto-convert like standard types.
💥 Example Error Message
error C2440: '=' : cannot convert from 'FVector' to 'float'
🛠️ Example Code
FVector Location = FVector(100.f, 200.f, 300.f);
float X = Location; // ❌ C2440: cannot convert from 'FVector' to 'float'
✅ How to Fix C2440 UE5 – Step-by-Step
✔️ 1. Use the Correct Member or Conversion
You can’t assign an entire struct (FVector
) to a float
. Use the specific member instead.
Fix:
float X = Location.X; // ✅ Access just the float you need
✔️ 2. Watch Out for FString vs const char*
This is a classic UE5 trap. You can’t assign a string literal ("hello"
) directly to an FString
.
Wrong:
FString Name = "Player"; // ❌ C2440: cannot convert from 'const char*' to 'FString'
Fix:
FString Name = FString(TEXT("Player")); // ✅ Correct way in UE5
✔️ 3. Casting Pointers or Object Types
Trying to convert between unrelated classes? You’ll need an explicit cast, and it has to be safe.
Wrong:
AActor Actor = SomeActorPointer; // ❌ assigning a pointer to an object
Fix:
AActor* Actor = SomeActorPointer; // ✅ use the correct pointer type
✔️ 4. Use Cast<>
for UE5 Class Conversions
Unreal provides the Cast<>()
function for converting between UObject-based types.
Fix:
AEnemy* Enemy = Cast(HitActor); // ✅ Runtime-safe casting
✅ Summary: How to Fix C2440 in UE5
Cause | Fix Example |
---|---|
Type mismatch (struct vs float) | Use specific member (Location.X ) |
CString vs FString | Wrap in FString(TEXT("...")) |
Pointer vs object | Use correct pointer type (AActor* , not AActor ) |
Class conversion | Use Cast<>() for UObject-based conversions |