Error: C2084 Already has a body

Hello!

I am trying to spawn in a blueprint via c++. And I have a problem with the class.
When I compile it says that AA_MainGame::AA_MainGame(const class FObjectInitializer& PCIP) already has a body. I read somewhere that this means that I’ve probably included the file to much. I’ve read this post on stackoverflow: c++ - Error C2084 'Function already has a body' - Stack Overflow and the article about header guards, and I do understand the concept but I do not know how to apply it to my own code.

.h

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

#pragma once

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

/**
 * 
 */
UCLASS()
class ROCKETGAME_API AA_MainGame : public AActor
{
	GENERATED_BODY()

		virtual void BeginPlay() override;
		TSubclassOf<AA_MainGame> BlueprintVar; // YourClass is the base class that your blueprint uses
	
	
};

.cpp

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

#include "RocketGame.h"
#include "A_MainGame.h"
#include "Engine.h"

AA_MainGame::AA_MainGame(const class FObjectInitializer& PCIP)
	: Super(PCIP)
{

	// Spawning the blueprint into the level
	static ConstructorHelpers::FObjectFinder<UBlueprint> BP_MainGame(TEXT("Blueprint'/Game/Blueprints/BP_MainGame.BP_MainGame'"));
	if (BP_MainGame.Object) {
		BlueprintVar = (UClass*)BP_MainGame.Object->GeneratedClass;
	}




}

void AA_MainGame::BeginPlay()
{
	Super::BeginPlay();

	UWorld* const World = GetWorld(); // get a reference to the world
	if (World) {
		// if world exists
		AA_MainGame* YC = World->SpawnActor<AA_MainGame>(BlueprintVar, FVector(0, 0, 0), FRotator(0, 0, 0));
	}
}

I hope you can help me out :slight_smile:

Thanks in advance,

NeusAap.

Hi NeusAap,

The Problem here is that the ‘GENERATED_BODY()’ Macro aleready declares and defines ‘AA_MainGame(const class FObjectInitializer& PCIP)’ for you, so when you define it in your *.cpp you have two Definitions.

But the Macro does not declare & define the Constructor if you declare it yourself, so you have to add ‘AA_MainGame(const class FObjectInitializer& PCIP);’ in your header and it should work.

–NoNoid

Hi,

I’ve placed just below the ‘GENERATED_BODY()’ Macro. After looking at my code I think that you should also make the Constructor public.

Does the Error change when you place the declaration in the Header?

–NoNoid

Hello,

Thanks for the quick respond. I get what you are trying to say, but when I try to apply it, I can not find out where to put it.

For me, it sounds the most logical to add it after: class ROCKETGAME_API AA_MainGame : public AActor. But that does not work, any other spot neither. Where do I place it?

Thanks again and sorry for the stupid questions :stuck_out_tongue:

NeusAap.

Aaaah,
Amazing! It works now :slight_smile:

Thanks allot! +1

It will now boot up, but the blueprint doesn’t spawn :P. Oh well… Atleast 1 error less to deal with :smiley: Thanks again!

Worked for me too! I almost gave up, thumbs up for you NoNoid!

Undying classic: a lot of mumbo-jumbo to implement a single blueprint overridden function…

Maybe the purpose is just to learn how to developp in C++, even if the same can be achieved quicker with a blueprint.
You should restrain from judging what people do or want to do, because you may not have a full understanding of their goals.

For me help changing GENERATED_BODY() by GENERATED_UCLASS_BODY()