C2664 with Cannot convert from U * to T *

I have created the following method:

AActor* AFPSCharacter::LookingAtCollectable()
{
	FHitResult HitRes;
	FRotator Rotation;
	FVector Location;

	GetController()->GetPlayerViewPoint(Location, Rotation);

	FVector Start = Location;
	FVector End = Start + Rotation.Vector() * ReachDistance;

	bool HasHit = ()->LineTraceSingleByChannel(HitRes, Start, End, ECC_Visibility, FCollisionQueryParams());

	if (HasHit)
	{
		AActor* actor = Cast<AActor>(HitRes.GetActor());

		if (actor->IsInA(ACollectable::StaticClass()))
		{
			return actor;
		}
	}

	return nullptr;
}

…Which is intended to return either nullptr if the user is not looking at a ‘Collectable’ or return a pointer to the actor otherwise. This method worked when the code was within the input handler (return actor; was replaced with actor->Destroy()). However, it is now raising the C2440 error as follows:


2>Building 6 actions with 8 processes...
2>  [1/6] FPSCharacter.cpp
2>C:\Program Files\Epic Games\UE_4.24\Engine\Source\Runtime\Core\Public\UObject/WeakObjectPtrTemplates.h(110): error C2440: 'initializing': cannot convert from 'U *' to 'T *'
2>          with
2>          [
2>              U=AActor
2>          ]
2>          and
2>          [
2>              T=AActor *
2>          ]
2>  C:\Program Files\Epic Games\UE_4.24\Engine\Source\Runtime\Core\Public\UObject/WeakObjectPtrTemplates.h(110): note: Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
2>  C:\Users\egrsi\Documents\Unreal Projects\FirstPerson\Source\FirstPerson\FPSCharacter.cpp(76): note: see reference to function template instantiation 'TWeakObjectPtr &TWeakObjectPtr::operator =(U *)' being compiled
2>          with
2>          [
2>              U=AActor
2>          ]
2>  C:\Users\egrsi\Documents\Unreal Projects\FirstPerson\Source\FirstPerson\FPSCharacter.cpp(76): note: see reference to function template instantiation 'TWeakObjectPtr &TWeakObjectPtr::operator =(U *)' being compiled
2>          with
2>          [
2>              U=AActor
2>          ]
2>C:\Program Files\Epic Games\UE_4.24\Engine\Source\Runtime\Core\Public\UObject/WeakObjectPtrTemplates.h(111): error C2664: 'FWeakObjectPtr &FWeakObjectPtr::operator =(const FWeakObjectPtr &)': cannot convert argument 1 from 'T *' to 'const UObject *'
2>          with
2>          [
2>              T=AActor *
2>          ]
2>  C:\Program Files\Epic Games\UE_4.24\Engine\Source\Runtime\Core\Public\UObject/WeakObjectPtrTemplates.h(111): note: Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
2>  C:\Program Files\Epic Games\UE_4.24\Engine\Source\Runtime\CoreUObject\Public\UObject/WeakObjectPtr.h(57): note: see declaration of 'FWeakObjectPtr::operator ='

According to documentation, the types of these functions match up fine and the linter doesn’t have issues with any of it either. I’m fairly new to Unreal Engine, would be great if someone could help. Thanks

Maybe your HitRes.GetActor() returns an array of actors?
Try to add a breakpoint and debug it, and it would be nice to see its data.

I can’t add any breakpoint, since the code itself won’t compile. The documentation specifies that <FHitResult>.GetActor() returns a AActor*, and the linter agrees so if it were a list I think there’d be bigger issues

HitRes.GetActor() already returns AActor* so casting it to AActor* is redundant and it may be cause of this error

Error occured regardless of cast. Submitting my own ‘Answer’ to this now…

I ‘solved’ this as follows:

  • Turned method to member procedure by setting return type to void
  • Added new attribute LookingAt
  • Replaced the main return in the member procedure with an assignment to LookingAt and removed the nullptr return

I haven’t a clue why this now works but it does so I’m not really going to complain.

Did u find out how u solved it? I encountered the same error but in my case I was doing this

Rocket->ProjectileMovement->HomingTargetComponent = TargetPawn;

where the type of HomingTargetComponent is USceneComponent and type of TargetPawn is APawn. So i think the error was about the incorrect Type, I’m new to coding and dont know the actual terms so idk how to say that but hope u get it .