Cyclic dependency on header files

I’ve run into an issue with cyclic dependency. Specifically, character needs to reference inventory, inventory also needs to reference character. I don’t think there’s any way to break this dependency without a lot of trouble either.

Right now, Inventory.h includes MyCharacter.h, MyCharacter.h includes inventory.h. The error given is “Class MyCharacter DependsOn(Inventory) is a circular dependency.” The way I see suggested to break this, and I’ve done before in pure C++, is to use forward declaration so the compiler knows that the class exists before it reaches the appropriate header file. Doing this changes nothing. Since there’s no VS error code, and it’s listed as “OtherCompilationError”, I’m pretty sure that it’s specifically a problem with the header files, and not actually with the classes.

This just makes me think I’m including files incorrectly. I tried looking at the sample shootergame to see how they do it. They have the same cycle with the character class referencing weapon and vice versa, but there’s almost no include files. Instead, there’s #include "ShooterGameClasses.h" in ShooterGame.h, and ShooterGame.h is included from other classes. ShooterGameClasses.h does not exist. Googling suggests that it’s generated by the UnrealHeaderTool, but I can’t find any specifics of what exactly it does. A -classes.h file does exist in my project, but is nearly blank:

// Copyright 1998-2015 Epic Games, Inc. All Rights Reserved.
/*=============================================================/*===========================================================================
	C++ class boilerplate exported from UnrealHeaderTool.
	This is automatically generated by the tools.
	DO NOT modify this manually! Edit the corresponding .h files instead!
===========================================================================*/
#pragma once

This is the entirety of that class. I’m assuming that UHT is supposed to look over all of my classes and automatically add #includes in here, then I just #include this generated file.

If that is the case, why isn’t UHT putting anything into this file? Is there something I need to do to manually run UHT and let it find my classes, beyond pressing “Compile” in Visual Studio? That sounds like it would resolve the specific issue of the cyclic include files, but what about the cyclic classes?

If this isn’t the case, then what is the proper way to resolve the cyclic include files and classes?

You really shouldn’t be running into any cyclic dependency issues if you’re including your header files correctly. I am assuming you’re referencing data types in one header file that is declared in the other?

I am reasonably certain that I am not including my header files correctly. And yes; more specifically, both header files reference a class declared in the other.

#Wiki For You

I polished and updated my Wiki on Forward Declaration, just forward declare your character class in your inventory class

#Solution For You in Exact Detail

include your inventory class at the top of your character class

then you should #include your character class only in the .cpp file of your inventory class, avoid trying to access your character class in your inventory .h, You use forward declaration to include a pointer to your character class in your inventory .h, but you dont access its vars in the .h, only in the .cpp

#Summary

inventory class .h → forward declares character class (see wiki)

inventory class. cpp → now you can #include character class and actually access its functions and vars

character class .h#include your inventory class, use inventory however you like in .h or .cpp

#Wiki Link

UE4 Forward Declaration

Rama

PS: dont mess with the UE4 auto-generated classes files, just do the above, you should make edits only to inventory and character classes at your project level.

Thanks. Moving the includes to the CPP files fixed the issue.

I wasn’t asking about manually changing the auto-generated files. I was asking about having the tool that updates them change them. The file I mentioned is completely blank, which seems odd to me. I don’t know if it’s supposed to be blank or not, because I can’t find any solid information on what that file is supposed to contain, or what UHT actually does.