Download

Beginner Question | Constructor | no appropriate default constructor available

Hello,

my Code doesn’t work. And I have no idea what to do to make it work. I would be so thankful if someone could help me.

So the actual code should ask from the Character.cpp the initialposition of the BrickPositionActor that is placed in the level.
But i get the error: no appropriate default constructor available

BrickPositonActor.h



#pragma once

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

UCLASS()
class ABrickPositionActor : public AActor
{
	GENERATED_UCLASS_BODY()

	//UPROPERTY(EditDefaultsOnly, Category = Position) //throws error: In BrickPositionActor: Unrecognized type 'void'!?!!?

	 const void GetPosition(FVector* blah);	
};


BrickPositionActor.cpp



#include "CodeThirdPerson.h"
#include "BrickPositionActor.h"

ABrickPositionActor::ABrickPositionActor(const class FPostConstructInitializeProperties& PCIP)
	: Super(PCIP)
{

}

const void ABrickPositionActor::GetPosition(FVector* blah)
{
	FVector blub;
	blub = GetActorLocation();
	blah->X = blub.X;
	blah->Y = blub.Y;
	blah->Z = blub.Z;
}


CodeThirdPersonCharacter.cpp



ACodeThirdPersonCharacter::ACodeThirdPersonCharacter(const class FPostConstructInitializeProperties& PCIP)
: Super(PCIP)
{
	ABrickPositionActor BrickPositionActor; //errormessage: no appropriate default constructor available

	FVector PositionBrickActorVector;
	BrickPositionActor.GetPosition(& PositionBrickActorVector);
        //Stuff
}


Somebody has an idea?

As far as I’m concerned you can’t create the actor the way your trying. :slight_smile:

I’m using this code to spawn actors:



UWorld* const World = GetWorld();

if(World)
{
  	FActorSpawnParameters SpawnParameters;
        TSubclassOf<class AYourActor> ActorReference;

        static ConstructorHelpers::FObjectFinder<UBlueprint> CHReference(TEXT("Blueprint'/Path/To/Your/Actor'")); //Take reference from editor - it doesn't have to be blueprint
		
	if(CHReference.Object)
	{
		ActorReference = (UClass*)CHReference.Object->GeneratedClass;
	}

	//Set up spawn parameters
	SpawnParameters.Instigator = GetPawn();

	//Spawn actor
	AYourActor* CreatedActor = World->SpawnActor<AYourActor>(ActorReference, Position, Rotation, SpawnParameters);

	if(CreatedActor)
	{
		//Actor successfully spawned
	}
}


Thank you Ogniok for that Code and your answer-
That will be helpful for my little project :).

But beside I’m doing it wrong, what is the point of this Error?
What is syntactically wrong?

Well, as yours constructor definition states, it takes one argument.



ABrickPositionActor::ABrickPositionActor(const class FPostConstructInitializeProperties& PCIP)


So you are saying that I should do something like


ABrickPositionActor::ABrickPositionActor(const class FPostConstructInitializeProperties& PCIP, const class ABrickPositionActor BrickPositionActor)

?

So I can’t initialisize my actor in the class?

Or what would be the code for the solution?

It would realy help me to understand the language/problem.

And thank you for your answer :slight_smile:

You can’t just create on object of an actor. You have to do it by spawning it in the world so the engine will know that it was created, store it in the right place in memory etc. And when engine constructs your objects it gives it FPostConstructInitializeProperties.