C++ Error when using UPROPERTY()

I encountered this problem and I have no idea how to solve this one.

I have this struct from RO_Stats.h

and from RO_AI.cpp I include the RO_Stats.h

345425-inlude.png

declare this in RO_AI.h

345428-forwarddeclare.png

345427-variable.png

Tried removing the UPROPERTY(), It compiled and error

I don’t know why UPROPERTY() is giving this error.

Error: : Unrecongnized type ‘FSubStat’ - type must be a UCLASS, USTRUCT, UENUM

I’m no expert, I really appreciate any help.

Hello! Do you provide a forward declaration of FSubStat in RO_AI.h? It is just a

struct FSubStat;

line just after include things

You dont need complete defenition for pointers in header (although I dont check auto includes, that are made by UHT)… But I dont know, if Unreal doesnt allow pointer to USTRUCT UProp, but that can be case…

Forward declarations won’t do for USTRUCTs, the UHT needs the complete definition of the struct when you want to use it in a reflected context. So if you declare UPROPERTY() FSubStat* subStats; in RO_AI.h you also need to include RO_Stats.h in the header file (not just the .cpp file). However, you will get another UHT error after that along the lines of ‘cannot have exposed pointer to this type’, because you cannot have a member UPROPERTY-pointer to a USTRUCT.

I kinda solved it, but I am not sure if this is the correct way of solving the problem.

I made a RO_Structure.h - moved every UENUM,USTRUCT to that header
just like this example, and include it to RO_AI.h so that my AI will have the same struct.
but if this is wrong please let me know, for the best solution

345591-include.png

yes you do. There is no way to forward declare a USTRUCT. This is not a compiler error but a UHT error, so it is not a C++ issue but a question of what the UHT (Unreal header tool) do and can’t do. They simply haven’t implemented forward declarations for USTRUCTs.

Including header files simply copy-pastes the code of the header file into the location where it is included, so it doesn’t matter whether you move them to a new header file and include that, or simply include the original header file where you had the struct definition before. The point is that the definition of the struct has to be there when you get to the line where you declare the member variable of that type.

I see, I hope they implement it.
Do you have any suggestion for this mr.GRIM? It would be very helpful.

As far as I know, this part is WRONG - I end up including the RO_Structure.h in both RO_AI.h and .cpp since they don’t know the type.

For now, this is what I did, Waiting for an option to achieve this.

But yeah, Thank you for the clarification about USTRUCT- ( There is no way to forward declare a USTRUCT )

What part are you referring to? what is WRONG?

As far as forward declaration of ustructs goes: there is no way around including the definition, but it is not really a problem because you cannot have a uproperty pointer to a ustruct anyway. ustructs are meant to be simple containers for data so there should not really be a reason to have member pointers to them and if you have a struct type variable as member (not just a pointer) you need the full definition of the struct anyway because the compiler needs to know the size of the struct to allocate the memory. I get the impression that you think it is a problem that you need to include other header files but it’s really not, we prefer to avoid it when possible because it reduces compilation times but sometimes it is necessary.

“What part are you referring to? what is WRONG?”
The part where I need to include the RO_Structure.h to other class .h and .cpp.

Thank you for the clarification - “we prefer to avoid it when possible because it reduces compilation times but sometimes it is necessary.”

This solves my problem then. Thank you again, MR.GRIM have a nice day!