Cannot inslude struct in AActor class

Using the various guides on the UE wiki and other sites, I’ve created a struct in it’s own .h file. After creating the struct, the project builds just fine, however, when I wish to use the struct in my AActor I’m given the error:

Cannot open include file: ‘…/Structs/ComponentStructure.h’: No such file or directory

This has me somewhat confused as the file is actually there.

Here’s the definition of my struct:

#pragma once

// Forward declarations
class AComponentMaster;

struct FComponentStruct

    float Price;

    float WorkMinutesRequired;

    TSubclassOf<AComponentMaster> NextComponent;

    // Constructor
    { }

Here’s where I include it, as I wish to use this struct:


#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "../Structs/ComponentMaster.h"
#include "ComponentMaster.generated.h

class COMPTECH_API AComponentMaster : public AActor

    // Constructor

    FComponentStruct ComponentStruct;

    // Etc...

My folder structure looks like this:

    - Comptech
        - Components
        - Structs

Hence the …/ in my include.

I simply cannot grasp why the compiler says it can’t find the struct file. Any help is highly appreciated, as I’m somewhat stuck until I resolve this issue.

Try with #include “Structs/ComponentStruct.h”

Tried it, but it just triggers a new error: Unrecognized type ‘FComponentStruct’ - type must be a UCLASS, USTRUCT or UENUM

Judging by your sources directory structure #include “Structs/ComponentStruct.h” should work just fine.

I don’t know if this was a typo in your post or if your file has also this error but in ComponentMaster.h you are including Structs/ComponentMaster.h instead of Structs/ComponentStruct.h

Yes, it was a typo, my bad :slight_smile: So according to you this should work? I just can’t get it to. Still getting the same error as the latest comment I wrote.

What happens if you replace GENERATED_USTRUCT_BODY() with GENERATED_BODY() and USTRUCT() with USTRUCT(BlueprintType)? Just try and lets see what happens.

Nothing changes, still the same error. Is it really true that it should be this difficult creating a struct? I can’t seem ti find the issue, and it’s a bit strange

One more thing, did you add the generated include in the header where you declared your struct?

#include "ComponentStruct.generated.h"

Yup, I did

Can you attach your project source? I just cannot figure out whats happening using what we have here.

You want all of it?

Before you do that, please paste here the whole compilation log, maybe we can resolve this by looking at it.

I think this might have something to do wiht the way I created the header file. I didn’t use the editor, but rather just created the header file and the subfolder “Struct” in Visual Studio. I’ve seen some question about this and what issues it may cause.

Folders in visual studio solution are not folders. Those are filters (virtual folders). If you add one inside visual studio the folder won’t be created on disk.

Exactly, which just lead me to a solution. The newly created header file was placed in the intermediate folder, instead of the source folder. If I were to create the header file in the source folder, it seems to work.

However, this doesn’t seem like the correct way to do it, or am I wrong?

When I need a simple header file for a struct definition I just create it in windows explorer (go to your project source, create the folder where you want to store it, right click, new text file, set its extension to .h)
That will work just fine. For actual classes I create them from the editor for convenience.
When you create the files from the windows explorer remember to add it to source control manually if you are using an scm.

Creating the header file in explorer, doesn’t make it show in VS. I suppose the project doesn’t see this file as included. How do you solve this? Just add existing item?

You have to regenerate project files. You can do it by right clicking your uproject file or from the file menu inside the editor.

PD: You can also create an empty class from editor, delete the class declaration and use that file to declare your struct.

I tried that already, didn’t seem to work as the cpp file is still in the intermediate folder (I think) and messes with the build, giving me compile errors.

PERFECT! This seems to solve my issue within VS. I still cannot see the struct in the editor though. Any solution to this? It’s not that I really intend to use it in blueprints, but since the other cpp classes are present in the editor, I’d like the struct to be there too.