Error C2440: '=' : cannot convert from 'TSubobjectPtrConstructor' to 'USphereComponent *'

Hello. Why every time the following error appears in my cpp file: error C2440: ‘=’ : cannot convert from ‘TSubobjectPtrConstructor’ to ‘USphereComponent *’
The code is as follows:
#include “GoldenEgg.h”
#include “NPC.h”
#include “MyHUD.h”
#include “Avatar.h”

ANPC::ANPC(const class FPostConstructInitializeProperties& PCIP) :
Super(PCIP)
{
ProxSphere = PCIP.CreateDefaultSubobject(this,TEXT(“Proximity Sphere”));
ProxSphere->AttachTo(RootComponent);
ProxSphere->SetSphereRadius(32.f);
// Code to make ANPC::Prox() run when this proximity sphere
// overlaps another actor.
ProxSphere->OnComponentBeginOverlap.AddDynamic(this,&ANPC::Prox);
NpcMessage = “Hi, I’m Abhishek”;
}

void ANPC::Prox(AActor* OtherActor,
UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep,
const FHitResult & SweepResult)
{
// if the overlapped actor is not the player,
// you should just simply return from the function
if (Cast(OtherActor) == nullptr)
{
return;
}
APlayerController* PController = GetWorld() ->GetFirstPlayerController();
if (PController)
{
AMyHUD * hud = Cast(PController->GetHUD());
hud->AddMessage(Message(NpcMessage, 5.f, FColor::White));
}
}

It’s quite hard to read your code in the above post as it’s not formatted, but the basic error is that a TSubobjectPtrConstructor and a USphereComponent are not the same thing. YSubobjectPtrConstructor is what the CreateDefaultSubobject will provide unless you state otherwise.

It’s been a while since I’ve used the PCIP.CreateDefaultSubobject calls, which is where I’m guessing the error is occurring, but it looks like you’re not providing the template with the required information.

Instead you should be explicitly stating what kind of component you are expecting, so:

 ProxSphere = PCIP.CreateDefaultSubobject(this,TEXT("Proximity Sphere"));

becomes:

 ProxSphere = PCIP.CreateDefaultSubobject<USphereComponent>(this,TEXT("Proximity Sphere"));

Also, it seems like you’re using quite an old version of the engine. Is this required for your project? You might do well to upgrade.

Firstly my apology for not formatting the program before posting. I just copied the program right from Visual Studio. Secondly even if I use the syntax: ProxSphere = PCIP.CreateDefaultSubobject(this,TEXT(“Proximity Sphere”)); the same error: error C2440: ‘=’ : cannot convert from ‘TSubobjectPtrConstructor’ to ‘USphereComponent *’ is popping up. And yes I am using an old version of the Engine. Version is 4.5 Rest of my project is working fine. I am just stuck in this very program.

Ah, that’s fine. I just thought I’d mention it was old in case you’d accidentally wound up with an old version.

Not sure if it’s a typo in your reply, the version you should be using is the one that contains the template information (i.e. the USphereComponent within <>):

ProxSphere = PCIP.CreateDefaultSubobject<USphereComponent>(this,TEXT("Proximity Sphere"));

Does that come up with the same error?

If not you could try casting it with either:

ProxSphere = (USphereComponent*)PCIP.CreateDefaultSubobject<USphereComponent>(this,TEXT("Proximity Sphere"));

or

ProxSphere = cast<USphereComponent>(PCIP.CreateDefaultSubobject<USphereComponent>(this,TEXT("Proximity Sphere")));

Also make sure in your .h file that ProxSphere is a pointer (i.e.

UPROPERTY(...)
class USphereComponent* ProxSphere;