CreateDefaultSubobject error assinging to UShapeComponent

Hi, I have created an Actor C++ component “PickUp”. But there are few lines that are throwing errors. I have declared UShapeComponent, UStaticMeshComponent, USceneComponent, OnPlayerEnterPickUpBox() functions

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "PickUp.generated.h"

UCLASS()
class YT_API APickUp : public AActor
{
	GENERATED_BODY()
	
public:	
	// Sets default values for this actor's properties
	APickUp();

protected:
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;

public:	
	// Called every frame
	virtual void Tick(float DeltaTime) override;

    UPROPERTY(EditAnywhere)
    USceneComponent* PickUpRoot;
    
	UPROPERTY(EditAnywhere)
    UStaticMeshComponent* PickUpMesh;
    
	UPROPERTY(EditAnywhere)
    UShapeComponent* PickUpBox;
    
    UFUNCTION()
    void OnPlayerEnterPickUpBox(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult );
};

This is the cpp files. Where all the code is written in constructor

#include "PickUp.h"


// Sets default values
APickUp::APickUp()
{
 	// Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;
    
    PickUpRoot = CreateDefaultSubobject<USceneComponent>(TEXT("PickupRoot"));
    RootComponent = PickUpRoot;
    
    PickUpMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("PickupMesh"));
    
    PickUpMesh->AttachToComponent(PickUpRoot, FAttachmentTransformRules::SnapToTargetNotIncludingScale);
    
    
    PickUpBox = CreateDefaultSubobject<UBoxComponent>(TEXT("PickupBox"));
    PickUpBox->SetWorldScale3D(FVector(1.0f, 1.0f, 1.0f));
    PickUpBox->bGenerateOverlapEvents = true;
    PickupBox->OnComponentBeginOverlap.AddDynamic(this, &APickUp::OnPlayerEnterPickupBox);
    PickUpBox->AttachToComponent(PickUpRoot, FAttachmentTransformRules::SnapToTargetNotIncludingScale);
}

// Called when the game starts or when spawned
void APickUp::BeginPlay()
{
	Super::BeginPlay();
	
}

// Called every frame
void APickUp::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);

}

void APickUp::OnPlayerEnterPickUpBox(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult )
{
    Destroy();
}

When I try to run this I get following errors:

Info Setting up Mono
Info /Users/Shared/Epic Games/UE_4.16/Engine /Users/Shared/Epic Games/UE_4.16/Engine/Binaries/Mac
Info Compiling game modules for hot reload
Info Parsing headers for YTEditor
Info   Running UnrealHeaderTool "/Users/abhimanyuaryan/Documents/Unreal Projects/YT/YT.uproject" "/Users/abhimanyuaryan/Documents/Unreal Projects/YT/Intermediate/Build/Mac/YTEditor/Development/YTEditor.uhtmanifest" -LogCmds="loginit warning, logexit warning, logdatabase error" -Unattended -WarningsAsErrors -installed
Info Reflection code generated for YTEditor in 7.82594 seconds
Info Performing 2 actions (8 in parallel)
Info [1/2] Compile PickUp.cpp
Info /Users/abhimanyuaryan/Documents/Unreal Projects/YT/Source/YT/PickUp.cpp:20:40: error: use of undeclared identifier 'UBoxComponent'; did you mean 'RootComponent'?
Info     PickUpBox = CreateDefaultSubobject<UBoxComponent>(TEXT("PickupBox"));
Info                                        ^~~~~~~~~~~~~
Info                                        RootComponent
Info Runtime/Engine/Classes/GameFramework/Actor.h:463:19: note: 'RootComponent' declared here
Info         USceneComponent* RootComponent;
Info                          ^
Info /Users/abhimanyuaryan/Documents/Unreal Projects/YT/Source/YT/PickUp.cpp:20:17: error: no matching member function for call to 'CreateDefaultSubobject'
Info     PickUpBox = CreateDefaultSubobject<UBoxComponent>(TEXT("PickupBox"));
Info                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Info /Users/Shared/Epic Games/UE_4.16/Engine/Source/Runtime/CoreUObject/Public/UObject/Object.h:81:15: note: candidate template ignored: invalid explicitly-specified argument for template parameter 'TReturnType'
Info         TReturnType* CreateDefaultSubobject(FName SubobjectName, bool bTransient = false)
Info                      ^
Info /Users/Shared/Epic Games/UE_4.16/Engine/Source/Runtime/CoreUObject/Public/UObject/Object.h:95:15: note: candidate template ignored: invalid explicitly-specified argument for template parameter 'TReturnType'
Info         TReturnType* CreateDefaultSubobject(FName SubobjectName, bool bTransient = false)
Info                      ^
Info 2 errors generated.

Hello AbhimanyuAryan,

I think you will change this line it’s work.

  UPROPERTY(EditAnywhere)
     UBoxComponent* PickUpBox;

can’t I assign BoxComponent to ShapeComponent?

As much as I know you can’t.

It’s called Polymorphism in C++. I guess you can refer to child objects from parent

ShapeComponent is parent & BoxComponent is a specific type of Shape(ie parent)

Sorry, I was new to learning C ++ but i find solution your issues. Problem is OnPlayerEnterPickupBox. You forget class type.

.cpp
void APickUp::OnPlayerEnterPickUpBox(class UPrimitiveComponent* OverlappedComp, class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
	Destroy();
}

.h
  UFUNCTION()
     void OnPlayerEnterPickUpBox(class UPrimitiveComponent* OverlappedComp, class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult);

I had typos in this code. Sorry fixed it

that’s keyword is optional. There were typos in my code. Fixed it…working now