Download

[C++] Everytime i want to #include something i get a : is a circular dependency error

Everytime i want to #include something i get a : is a circular dependency error. What the hell is this and how do i fix it ?

Hi,
Easy(ish) way to see what’s happening is to create UML of those files you including and see where that circular dependency happens.

i have no idea what an uml file is or how i can fix it with it.

just add "#pragma once " on the top of your header file.

already there. if i double click on the error it say that this error acures with the UCLASS().

Try using forward class declarations instead, it’ll also prevent having enormous pre-compiled headers too.

am iam doing this right ? I get another error that says : use of undefined type “myclassname”

heres thje header file:



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

#pragma once

#include "BaseBuilding.h"
#include "SimpleCraftingHub.generated.h"

class ABaseUnit;

UCLASS()
class PROJECTTECH_API ASimpleCraftingHub : public ABaseBuilding
{
	GENERATED_BODY()
public:
	//Max 9
	UPROPERTY(EditAnywhere, Category = Crafting)
	TArray<TSubclassOf<ACraftedItem>> CraftableItems;
	
	TArray<TSubclassOf<ABaseItem>> Storage;

	bool open;

	void Open();
	void Craft(FName name);
	void addItem(TSubclassOf<ABaseItem> item);

private:

	ACraftedItem* currentTask;
	ABaseUnit* GetFreeWorkers();

};


Hmm. Not sure if it’s this but try the following. Also, try to use the





headers when pasting code on the forums :slight_smile:

TArray<TSubclassOf<class ABaseItem>> Storage;

oh iam sorry ^^ i forgot to show you where the errors are in the cpp file.


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

#include "ProjectTech.h"
#include "SimpleCraftingHub.h"

void ASimpleCraftingHub::Open()
{
	open = !open;
}

void ASimpleCraftingHub::Craft(FName name)
{
	if (GetFreeWorkers())
	{
		
	}
}

ABaseUnit* ASimpleCraftingHub::GetFreeWorkers()
{
	for (TActorIterator<AActor> ActorItr(GetWorld()); ActorItr; ++ActorItr)
	{
		if (ActorItr->ActorHasTag("Worker"))
		{
			ABaseUnit* unit = Cast<ABaseUnit>(*ActorItr);
			if (unit->idle//heres the error it says iam trying to access an undefind type of ABaseUnit){ return unit; break; }
			else{ UE_LOG(LogTemp, Warning, TEXT("Busy")); }
		}
	}
	return NULL;
}

ASimpleCraftingHub::addItem(TSubclassOf<ABaseItem> item)//also iam getting an error here
{
	
}


The forward declaration only tells the compiler that BaseUnit is a class. This is useful to refer to a class in a header file without having to include the full class header (often avoiding circular dependencies), but it won’t allow you to actually use the class (since at this point the compiler doesn’t know anything about the actual members of the class).

So what you should do in your case is to include the BaseUnit header in the CPP file that uses it. You shouldn’t get circular dependency errors from including a header in a CPP file. I don’t think you need the forward declaration since you don’t seem to refer to ABaseUnit anywhere in the header.

Not a “code virtuosi” here.

Do you have ABaseUnit already defined (meaning an own .h and .cpp)?
Do you really “need” that both classes get methods that returns each other types? (in such way they need to include each other headers right from .h)

Well, .cpps doesn’t shoots circular references, I didn’t use the forward declaration technique, so another way to avoid the error is think on another methods to implement the class/method or use a third class that “connects and manipulates” both classes without ever being referenced inside the “manipulated” classes.

Maybe this will bring some additional ideas. :wink:

PE: Ow… And thanks to the ones that did mention the Forward Declaration, I’ve seen this a lot of times on Engine code, but never thought this could be used on “upper” levels.