Casting AController to AAIController results in error LNK2019: unresolved external symbol

I have a fairly simple function I just started writing inside an actor component:

void Uai_patrol_cpp::NextPatrolNode()
{
	APawn* pawn = Cast<APawn>(GetOwner());
	AController* controller = pawn->GetController();
	AAIController* aiController = Cast<AAIController, AController>(controller);
}

The third line with the cast is causing UE5 to throw on compile like so:

ai_patrol_cpp.cpp.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) private: static class UClass * __cdecl AAIController::GetPrivateStaticClass(void)" (__imp_?GetPrivateStaticClass@AAIController@@CAPEAVUClass@@XZ) referenced in function "protected: void __cdecl Uai_patrol_cpp::NextPatrolNode(void)" (?NextPatrolNode@Uai_patrol_cpp@@IEAAXXZ)
  Hint on symbols that are defined and could potentially match:
    "__declspec(dllimport) private: static class UClass * __cdecl APawn::GetPrivateStaticClass(void)" (__imp_?GetPrivateStaticClass@APawn@@CAPEAVUClass@@XZ)
    "__declspec(dllimport) private: static class UClass * __cdecl UCameraComponent::GetPrivateStaticClass(void)" (__imp_?GetPrivateStaticClass@UCameraComponent@@CAPEAVUClass@@XZ)
    "__declspec(dllimport) private: static class UClass * __cdecl UInputSettings::GetPrivateStaticClass(void)" (__imp_?GetPrivateStaticClass@UInputSettings@@CAPEAVUClass@@XZ)
    "__declspec(dllimport) private: static class UClass * __cdecl UObject::GetPrivateStaticClass(void)" (__imp_?GetPrivateStaticClass@UObject@@CAPEAVUClass@@XZ)
    "__declspec(dllimport) private: static class UClass * __cdecl UProjectileMovementComponent::GetPrivateStaticClass(void)" (__imp_?GetPrivateStaticClass@UProjectileMovementComponent@@CAPEAVUClass@@XZ)
    "__declspec(dllimport) private: static class UClass * __cdecl USkeletalMeshComponent::GetPrivateStaticClass(void)" (__imp_?GetPrivateStaticClass@USkeletalMeshComponent@@CAPEAVUClass@@XZ)
    "__declspec(dllimport) private: static class UClass * __cdecl USphereComponent::GetPrivateStaticClass(void)" (__imp_?GetPrivateStaticClass@USphereComponent@@CAPEAVUClass@@XZ)

If the pawn’s AController isn’t an AAIController (though I can’t imagine why it wouldn’t be), I would expect it to throw on a bad cast and not a linker error. It sounds to me like it’s saying Cast() is trying to call GetPrivateStaticClass() and it’s an ambiguous reference. Since I’m not the one calling GetPrivateStaticClass(), I’m not sure what it wants from me to fix that.

I tried deleting the ‘Intermediate’ folder and regenerating the VS project thinking that would shake something loose, but no.

Has anyone else run into this? Am I simply missing an include somewhere?

You must add AIModule to your Build.cs dependencies.

1 Like

Greetings @NightmareGames

I saw that Chatouille provided some information on a solution for your problem. Were you still experiencing the issue and needing assistance? If so, I’d be happy to help! If not, feel free to mark the solution resolved as to help other designers who may search for the same issue and find this solution. Thank you!

@Chatouille That fixed it. I wasn’t familiar with Unreal’s modules. Thanks!

@FrostyJas This evening was my first opportunity to revisit the problem. Is there some urgency behind this?

I’m glad that you were able to get it resolved. Great tip @Chatouille !

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.