Download

Error C2248 while trying to access a public constructor

Hi
I’ve recently started a new project and I found an error in my code that I’m unable to fix. I have the following actor class for power ups:

PowerUp.h:




#pragma once

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

UCLASS()
class MYPROJECT_API APowerUp: public AActor
{
    GENERATED_BODY()
   
    public:

        APowerUp();
        APowerUp(FString name, float duration);

    UPROPERTY(EditAnywhere)
        FString name = "";

    UPROPERTY(EditAnywhere)
        float duration = 0;

    protected:

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

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



PowerUp.cpp:




#include "PowerUp.h"

APowerUp::APowerUp() {

    // Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
    PrimaryActorTick.bCanEverTick = false;

}

APowerUpt::APowerUp(FString name, float duration) {

    this->name = name;
    this->duration = duration;

}

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

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



As you can see it’s very simple. However, when I try to create an actor of this class from another class I’m getting the following error (C2248):

I don’t know what I’m doing wrong as this is the first time I find this error

What are you calling to create the actor?

Just a heads up, this constructor:



APowerUp(FString name, float duration);


Is likely useless. Actor’s need to be created through a UWorld’s SpawnActor method.



GetWorld()->SpawnActor<APowerUp>(...);


Which always calls the default constructor - so the one with out parameters (or with the implied FObjectInitializer parameter). AFAIK there is no way to pass parameters to your constructor for UObjects (which includes Actors). If you try and new them up yourself, you’re allocating an object outside of the UObject memory management lifecycle which is going to cause no end of headaches.

You can just wrap that logic into some method (e.g., “InitializePowerUp”) and call it manually after you spawn the actor through the normal system.