C++ code works in 4.13.2 but doesn't work in 4.16.1

This is a PickUp code. I can pick up Mesh Object from game. I have added an actor “PickUp” & below are the header file & cpp file for this code

Cpp file ::::::::

#include "MyActor.h"


// Sets default values
AMyActor::AMyActor()
{
 	// 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, &AMyActor::OnPlayerEnterPickUpBox);
    PickUpBox->AttachToComponent(PickUpRoot, FAttachmentTransformRules::SnapToTargetNotIncludingScale);
}

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

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

}

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

Header File :::::

#pragma once

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

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

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

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

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

ERRORS :::::::::::::::::::::::::::::::::::::::::

Info Performing 3 actions (8 in parallel)
Info [2/3] Compile MyActor.cpp
Info [1/3] Compile MyProject.generated.cpp
Info /Users//Documents/Unreal Projects/4.16/MyProject/Source/MyProject/MyActor.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//Documents/Unreal Projects/4.16/MyProject/Source/MyProject/MyActor.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 /Users//Documents/Unreal Projects/4.16/MyProject/Source/MyProject/MyActor.cpp:21:14: error: member access into incomplete type 'UShapeComponent'
Info     PickUpBox->SetWorldScale3D(FVector(1.0f, 1.0f, 1.0f));
Info              ^
Info /Users/Shared/Epic Games/UE_4.16/Engine/Source/Runtime/Engine/Classes/AI/Navigation/NavigationTypes.h:490:42: note: forward declaration of 'UShapeComponent'
Info         void UpdateWithCollisionComponent(class UShapeComponent* CollisionComponent);
Info                                                 ^
Info /Users//Documents/Unreal Projects/4.16/MyProject/Source/MyProject/MyActor.cpp:22:14: error: member access into incomplete type 'UShapeComponent'
Info     PickUpBox->bGenerateOverlapEvents = true;
Info              ^
Info /Users/Shared/Epic Games/UE_4.16/Engine/Source/Runtime/Engine/Classes/AI/Navigation/NavigationTypes.h:490:42: note: forward declaration of 'UShapeComponent'
Info         void UpdateWithCollisionComponent(class UShapeComponent* CollisionComponent);
Info                                                 ^
Info /Users//Documents/Unreal Projects/4.16/MyProject/Source/MyProject/MyActor.cpp:23:14: error: member access into incomplete type 'UShapeComponent'
Info     PickUpBox->OnComponentBeginOverlap.AddDynamic(this, &AMyActor::OnPlayerEnterPickUpBox);
Info              ^
Info /Users/Shared/Epic Games/UE_4.16/Engine/Source/Runtime/Engine/Classes/AI/Navigation/NavigationTypes.h:490:42: note: forward declaration of 'UShapeComponent'
Info         void UpdateWithCollisionComponent(class UShapeComponent* CollisionComponent);
Info                                                 ^
Info /Users//Documents/Unreal Projects/4.16/MyProject/Source/MyProject/MyActor.cpp:24:14: error: member access into incomplete type 'UShapeComponent'
Info     PickUpBox->AttachToComponent(PickUpRoot, FAttachmentTransformRules::SnapToTargetNotIncludingScale);
Info              ^
Info /Users/Shared/Epic Games/UE_4.16/Engine/Source/Runtime/Engine/Classes/AI/Navigation/NavigationTypes.h:490:42: note: forward declaration of 'UShapeComponent'
Info         void UpdateWithCollisionComponent(class UShapeComponent* CollisionComponent);
Info                                                 ^
Info 6 errors generated.
Info ERROR: UBT ERROR: Failed to produce item: /Users//Documents/Unreal Projects/4.16/MyProject/Binaries/Mac/UE4Editor-MyProject-9730.dylib

Which use Visual Studio version? Because unsupported Visual Studio 2013 with UE 4.16

I’m on Xcode OSX

also same problem with Bootcamp Visual Studio 2017

I believe this is because you need to include the headers for the classes that you are using.

Info /Users//Documents/Unreal Projects/4.16/MyProject/Source/MyProject/MyActor.cpp:20:40: error: use of undeclared identifier 'UBoxComponent'; did you mean 'RootComponent'?

This is telling you that the compiler doesn’t know about the UBoxComponent class.

Try adding #include "Components/BoxComponent.h" to your cpp file includes. You may also have to include other header files such as the USceneComponent etc.

can I also include that in Header File?

Yes if you want to. It’s usually better practice to forward declare the variable in your header by adding class in front of the variable type and just including the header in the cpp file. That way when you include your classes header elsewhere you aren’t also including its deoendicies.