Introduction to UE4 Programming Error

Hello All,
Decided to get my feet wet with game designing, and I’m having a few struggles thus far. I’m working on completing the APickup introduction that’s on Youtube, and I’m getting a error to which I just can’t seem to solve. Note: I am use 4.7.

Below is my code, and the 2 errors I keep running into to are

  1. Error 6 error C2535: ‘APickup::APickup(const FObjectInitializer &)’ : member function already defined or declared

Pickup.h


// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "GameFramework/Actor.h"
#include "Pickup.generated.h"

UCLASS()
class MYPROJECTTUTORIAL_API APickup : public AActor
{
	GENERATED_UCLASS_BODY()

public:


		// True when the pickup is able to be picked up, false if soemething deactives the pickuped.
		UPROPERTY(EditAnywhere, BluePrintReadWrite)
		bool bIsActive;

		//Simple collision primitive to use as the root component.
		UPROPERTY(VisibleDefaultsOnly, BluePrintReadOnly)
		class USphereComponent* BaseCollisionComponent;

		// StaticMeshComponent to represent the pickup in the level.
		UPROPERTY(VisibleDefaultsOnly, BluePrintReadOnly)
		class UStaticMeshComponent* PickupMesh;

		//Function to call when the pickup is collected. 
		UFUNCTION(BlueprintNativeEvent)
		void OnPickedUp();

		// Sets default values for this actor's properties
		APickup(const FObjectInitializer& ObjectInitializer);

	// Called when the game starts or when spawned
	virtual void BeginPlay() override;
	
	// Called every frame
	virtual void Tick( float DeltaSeconds ) override;

	
	
};


Pickup.cpp



// Fill out your copyright notice in the Description page of Project Settings.

#include "MyProjectTutorial.h"
#include "Pickup.h"


// Sets default values
APickup::APickup(const FObjectInitializer& ObjectInitializer)
	: Super(ObjectInitializer)
{
 	// 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;
	//The pickup is valid when it is created
	bIsActive = true;

	BaseCollisionComponent = ObjectInitializer.CreateDefaultSubobject<USphereComponent>(this, TEXT("BaseSphereComponent"));

	RootComponent = BaseCollisionComponent;

	PickupMesh = ObjectInitializer.CreateDefaultSubobject<UStaticMeshComponent>(this, TEXT("PickupMesh"));

	PickupMesh->SetSimulatePhysics(true);

	PickupMesh->AttachTo(RootComponent); 

	PrimaryActorTick.bCanEverTick = true;


}
void APickup::OnPickedUp_Implementation()
{
	//There is no default behavior for the pickup
}

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


}

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

}



Thanks for your help in advance.

Yeah, those videos are a bit out of date. They were created even before 4.6.1 and we’re now at 4.7.X

There’s been a bit of a change to the way constructors are done and the GENERATED_UCLASS_BODY() has been deprecated on 4.6

Change GENERATED_UCLASS_BODY() to GENERATED_BODY()

Your constructor doesn’t need parameters anymore. You can now use:
APickup();
within your header file.

The CPP file implementation will change a bit as well. Rather than using ObjectInitializer for CreateDefaultSubobject<>(), you can now just call it directly like so:

BaseCollisionComponent = CreateDefaultSubobject<USphereComponent>(TEXT(“BaseSphereComponent”));

Note: the “this” parameter has also been removed, the object type is implied by the template parameter.

Thank you so much Slayemin. It worked beautifully with those changes. I’m new to C++, and even newer to UE4. So the API’s throw me off a tad. Since their C++ coding seems to be a tad bit different than regular C++. Appreciate the help .

The real issue here was that the generated header file already included the constructor declaration, so by putting



// Sets default values for this actor's properties
APickup(const FObjectInitializer& ObjectInitializer);


in your header, you were declaring it twice. The change as described by Slayemin above will fix it, due to the changes with GENERATED_BODY, but it’s also important that you understand why you ran into the issue to begin with. “Pickup.generated.h” is autogenerated by UE4 and contains a lot of boilerplate code. You’re likely to have the problem again in some form or another (like with replication) if you don’t know that it generates some function declarations automatically for you. Hopefully this clears up the why of the solution provided by Slayemin.

Thank you so much for this. I just started the Introduction to UE4 Programming videos today and got to this point. I am glad to finally find a solution and understand a bit about why it happens.