Compiler says that it can't recognize type

Hi, I’ve been struggling with this for quite some time now and I had no success solving it. I had this exact same problem with the exact same file and include but I don’t remember how I fixed it.

The compiler says “Unrecognized type ‘FInvItem’ - type must be UCLASS, USTRUCT or UENUM”
this is the code where the error occurs:

Pickup.h:

public:

	UPROPERTY(EditAnywhere, Category="Item Info")
		FInvItem itemInfo;

Pickup.h has the following includes:

#include "Interactable.h"
#include "ItemsDB.h"
#include "Pickup.generated.h" 

ItemsDB.h (is way bigger than this)

USTRUCT(BlueprintType)
struct FInvItem
{

	GENERATED_BODY()

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Item Info")
		int32 itemID = 0; //ID of the item in the items DB

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Item Info")
		int32 usesLeft = 0;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Item Info")
		FConsumable consumableInfo;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Item Info")
		FWeapon weaponInfo;

	/*UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Item Info")
		UCommonWidget* slotWidget;*/
};

ItemsDB.h has the following includes:

#include "Engine/DataAsset.h"
#include "GameCharacter.h"
#include "CommonWidget.h"
#include "Pickup.h"
#include "ItemsDB.generated.h"

I think I solved this problem before with forward declaring the ItemsDB in Pickup.h like this:

class UItemsDB;

but if I do this (the forward declaring of ItemsDB class in Pickup.h) now the GameCharacter.h gives me a bunch of errors related to structs from ItemsDB.h

Here are the includes in GameCharacter.h

#include "GameFramework/Character.h"
#include "Engine.h"
#include "ItemsDB.h"
#include "CommonWidget.h"
#include "Runtime/UMG/Public/Blueprint/UserWidget.h"
#include "Pickup.h"
#include "GameCharacter.generated.h"

Thanks in advance!

Thank you! And… what did you mean by “more fundamental code changes”?

Just that if you can’t solve your issue by splitting up your code into more modular headers, that you may instead need to re-design parts of it to resolve the interdependency between your types.

Pickup.h includes ItemsDB.h, and ItemsDB.h includes Pickup.h. This is a cycle, and is what causes your missing type error.

You’ll have to update your code to remove this cycle, either by splitting your structs into more headers, or by more fundamental code changes. Sadly you can’t use forward declarations with USTRUCT types since they can’t be pointers so the compiler needs to know the size up-front (forward declarations work with UCLASS types though).

You said that UCLASS can be forward declared, so to resolve my problem I shouy]ld be able to just forward declare the APickup class in ItemsDB.h but when I do it gives me a bunch of errors.

I can’t really answer that question because I don’t know how you’re using APickup in your ItemsDB.h file, nor do I know what your “bunch of errors” say.

I can say that forward declarations work with pointers or references to a a type because pointers and references are a fixed size, but that once you need to call a function or access some data from that type, the compiler needs to have seen the full type declaration. Thus if you forward declare a type in one header, you must have included that types declaration before you actually use it (typically you’d forward declare it in the .h, and include it in the cpp).