"Using incorrect object initializer" when compiling an inherited script

The situation is i have been trying to compile a class that inherits from a custom pawn class.
The .h File:

#pragma once

#include "CoreMinimal.h"
#include "BasePawn.h"
#include "Tank.generated.h"

/**
 * 
 */
UCLASS()
class TOONTANKS_API ATank : public ABasePawn
{
	GENERATED_BODY()

public:
	ATank();

private:
	UPROPERTY(EditAnywhere)
	class USpringArmComponent* SpringArm;

	UPROPERTY(EditAnywhere)
	float Speed = 200.0f;	
};

The .cpp file:

#include "Tank.h"
#include "GameFramework/SpringArmComponent.h"
#include "Camera/CameraComponent.h"

ATank::ATank()
{
	SpringArm->CreateDefaultSubobject<USpringArmComponent>(TEXT("Spring"));
	SpringArm->SetupAttachment(RootComponent);
}

The crash report shows:

Fatal error: [File:D:/Build/++UE4/Sync/Engine/Source/Runtime/CoreUObject/Private/UObject/Obj.cpp] [Line: 114] Using incorrect object initializer.

UE4Editor_CoreUObject
UE4Editor_ToonTanks!ATank::ATank() [C:\1.) D. GAMEDEV Folder\Unreal Engine Projects\ToonTanksProjectSetup_4.25\ToonTanksProjectSetup_4.25\ToonTanks\Source\ToonTanks\Tank.cpp:11]
UE4Editor_CoreUObject
UE4Editor_CoreUObject
UE4Editor_CoreUObject
UE4Editor_CoreUObject
UE4Editor_Core
UE4Editor_Core
UE4Editor_Projects
UE4Editor_Projects
UE4Editor
UE4Editor
UE4Editor
UE4Editor
UE4Editor
UE4Editor
UE4Editor
kernel32
ntdll

Im not sure what im doing wrong. im sure my syntax is correct. If anyone posts to assist, i’ll try to respond asap
Thanks in advance!

Realized my mistake:
I was using

SpringArm->CreateDefaultSubobject<USpringArmComponent>(TEXT("Spring");

which should be:

SpringArm->CreateDefaultSubobject<USpringArmComponent>(TEXT("Spring");

3 Likes

I found this and noticed that your solution has a typo, as it’s the same before/after.

For anyone that has this issue, instead of calling a function on the dereferenced springarm, you should be setting it equal to the default subobject:

SpringArm = CreateDefaultSubobject(TEXT(“Spring”);

9 Likes

sorry , but whats the difference between two

1 Like

thank you, and here i was thinking what i did wrong when initializing the root component lol

SpringArm=CreateDefaultSubobject(TEXT(“Spring”);

2 Likes