x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

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: http://stackoverflow.com/questions/21080453/error-c2084-function-already-has-a-body 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 :)

Thanks in advance,

NeusAap.

Product Version: Not Selected
Tags:
more ▼

asked Jan 03 '15 at 06:12 PM in C++ Programming

avatar image

NeusAap
70 5 7 11

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

2 answers: sort voted first

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

more ▼

answered Jan 03 '15 at 06:25 PM

avatar image

NoNoid
106 4 6 14

avatar image NeusAap Jan 03 '15 at 07:29 PM

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 :P

NeusAap.

avatar image NoNoid Jan 03 '15 at 07:53 PM

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

avatar image NeusAap Jan 03 '15 at 08:01 PM

Aaaah, Amazing! It works now :)

Thanks allot! +1

avatar image NeusAap Jan 03 '15 at 08:03 PM

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

avatar image otto001 Sep 07 '15 at 07:02 PM

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

avatar image Two-faced Sep 03 '18 at 11:58 AM

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

avatar image Azdoph Nov 17 '18 at 03:14 AM

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.

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

For me help changing GENERATED_BODY() by GENERATED_UCLASS_BODY()

more ▼

answered Nov 19 '18 at 05:28 PM

avatar image

kaiZer_Drago
42 5 3

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question