# Resolving Circular Dependency

**URL:** <https://forums.unrealengine.com/t/resolving-circular-dependency/296366>\
**Category:** C++\
**Tags:** dependencies, shooter-game, header, unreal-engine, UE4, CPP, question\
**Created:** [October 16, 2014, 5:51pm UTC](https://forums.unrealengine.com/t/resolving-circular-dependency/296366 "2014-10-16T17:51:31Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Janyx](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Janyx](https://forums.unrealengine.com/u/Janyx)\
**Post date:** [October 16, 2014, 5:51pm UTC](https://forums.unrealengine.com/t/resolving-circular-dependency/296366/1 "2014-10-16T17:51:31Z")

</div>

Can anyone help me understand how this works? I am trying to implement the LastTakeHitInfo feature in my game. I was looking at the ShooterGame example to see how they did it. I am running into a circular dependency issue and I am not sure why the Shoot Game works and mine doesn’t.

ArenaCharacter.cpp

```
#include "ArenaTypes.h"
#include "ArenaDamageType.h"
#include "GameFramework/Character.h"
#include "ArenaCharacter.generated.h"

UCLASS(Abstract)
class AArenaCharacter : public ACharacter
{
	GENERATED_UCLASS_BODY()

    (...)

    /** Replicate where this pawn was last hit and damaged */
	UPROPERTY(Transient, ReplicatedUsing = OnRep_LastTakeHitInfo)
	struct FTakeHitInfo LastTakeHitInfo;

    (...)

}

```

Because ArenaCharacter implements “struct FTakeHitInfo LastTakeHitInfo” it needs to include #include “ArenaTypes.h”. There is a problem though

ArenaTypes.h

```
#include "Object.h"
#include "ArenaCharacter.h"
#include "GameFramework/DamageType.h"
#include "ArenaTypes.generated.h"

#pragma once

USTRUCT()
struct FTakeHitInfo
{
	GENERATED_USTRUCT_BODY()

        (...)

        /** Who hit us */
	    UPROPERTY()
	    TWeakObjectPtr<class AArenaCharacter> PawnInstigator;

        (...)

}

```

The pawn investigator needs to know about AArenaCharacter. If I don’t include #include “ArenaCharacter.h” I get the error: TWeakObjectPtr can only be constructed with Object types. But obviously when I add #include “ArenaCharacter.h” header it creates a circular dependency. This is how the Shoot game works. What am I missing?

---

<div class="post-metadata">

**Author:** ![Steve\_Robb](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Steve\_Robb](https://forums.unrealengine.com/u/Steve_Robb)\
**Post date:** [October 20, 2014, 4:11pm UTC](https://forums.unrealengine.com/t/resolving-circular-dependency/296366/2 "2014-10-20T16:11:25Z")

</div>

Hi Ky,

Do you really have AArenaCharacter defined in ArenaCharacter.cpp file, as you have suggested? Any marked-up (i.e. UCLASS, USTRUCT etc.) types must be defined in a .h file of the same name as the type, but without the prefix - i.e. ArenaCharacter.h in your case - and in a Public, Private or Classes folder located underneath your module folder.

Steve

---

<div class="post-metadata">

**Author:** ![Janyx](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Janyx](https://forums.unrealengine.com/u/Janyx)\
**Post date:** [October 20, 2014, 5:05pm UTC](https://forums.unrealengine.com/t/resolving-circular-dependency/296366/3 "2014-10-20T17:05:47Z")

</div>

I figured it out. In the ShooterGame example in the ShooterGame.h they #include a header file called ShooterGameClasses.h this file contained a #include to every class in the project. Its is actually auto generated by the Unreal Engine. I looked in my project and found I too have a MyProjectClasses.h however mine was empty. You can’t add anything to that file since it is auto generated and will delete anything you add to it. I instead added all the includes into MyProject.h then I simple #include MyProject.h for every classes. This will insure no circular dependencies are created even when two classes refence each other.

---

<div class="post-metadata">

**Author:** ![Janyx](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Janyx](https://forums.unrealengine.com/u/Janyx)\
**Post date:** [October 20, 2014, 5:06pm UTC](https://forums.unrealengine.com/t/resolving-circular-dependency/296366/4 "2014-10-20T17:06:43Z")

</div>

Yes it is defined, I actually just figured out what the problem was, thanks though!

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_5bf0957e](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_5bf0957e](https://forums.unrealengine.com/u/anonymous_user_5bf0957e)\
**Post date:** [July 26, 2016, 1:44pm UTC](https://forums.unrealengine.com/t/resolving-circular-dependency/296366/5 "2016-07-26T13:44:38Z")

</div>

For me it also worked to set the default value in the constructor in the .cpp instead of directly in the .h file.

---

<div class="post-metadata">

**Author:** ![MWTAHM](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@MWTAHM](https://forums.unrealengine.com/u/MWTAHM)\
**Post date:** [September 28, 2019, 6:02pm UTC](https://forums.unrealengine.com/t/resolving-circular-dependency/296366/6 "2019-09-28T18:02:03Z")

</div>

thank you so much.  
Worked for me 🙂

---

<div class="post-metadata">

**Author:** ![MWTAHM](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@MWTAHM](https://forums.unrealengine.com/u/MWTAHM)\
**Post date:** [September 28, 2019, 6:02pm UTC](https://forums.unrealengine.com/t/resolving-circular-dependency/296366/7 "2019-09-28T18:02:23Z")

</div>

thank you so much.  
Worked for me 🙂

---

<div class="post-metadata">

**Author:** ![Rahulnm1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Rahulnm1](https://forums.unrealengine.com/u/Rahulnm1)\
**Post date:** [October 15, 2023, 9:11pm UTC](https://forums.unrealengine.com/t/resolving-circular-dependency/296366/8 "2023-10-15T21:11:45Z")

</div>

Hello, can you please help with that? I tried to use all includes in my project.h file and then I used include project.h in both header files. Can you please explain me with the example. Please help me out with this, I am stuck here as I cannot use forward declaration because I have to store reference of each other for further use.
