Struct unknown size

Hello,

I’m currently working on an inventory system, I need to make an Array out of my struct, this is the code of the struct:

and this is the code of the array:

I’m new to c++ so It’s probably not the cleanest code you’ve ever seen, the problem is, I get this error:

331322-3.png

And no matter what I try or search, I can’t find a solution. Can someone help me?

1 Like

Hello! Can you recheck include header lists?

These are the includes

it means compiler don’t know memory size of your struct, because it most likely don’t have full deceleration of it to know proper size. In .cpp file you should have include both header file with deceleration of struct before the class it self.

You might also have name conflict as in UE4 there already struct with FItemData:

That might be the case as you not include that you struct header in class header yet it see the struct. You should forward refrence in class header like this:

TArray<struct FStructName> MyArray;

then by including struct before class header file in cpp it will prevent potential circle refrence

4 Likes

I’ve changed the name to FItemVariables and included the header file where the struct is made. I also forgot to mention that the struct is made in Base_Item.h and referenced in an array in PlayerCharacter.h, after including the script in the header I got a few new errors, as I said I’m pretty new to c++, could you explain how to define a size to an struct?

You don’t have to define the size of the struct. The compiler knows the size automatically but it needs the definition of the struct in every translation unit (== .cpp file) the type is being used in. As stated, you probably have not included the definition of your struct in a source file where you use it. It would be much more helpful if you showed us the line where the error occurs instead of just the definition of your type (click on the error in your error list and vs will take you to the line in your code that causes the error).

Ok sry I didn’t see that. In 4.png you have declared TArray Inventory; but you have only forward declared your type in that header (struct FItemData; at the top). The compiler needs to know the full size of FItemData (or whatever you have renamed it to) to define your Inventory array. This means it needs to see the full definition of FItemData in that translation unit, so you have to put #include “Base_Item.h” (or whatever .h file you have defined it in) at the top as well.

The cause of the error is the array (the little screenshot in the post, and 4.png is the shot of the includes of the script where the array is made.

I figured it out, I accidentally put Replicated in the uproperty of the array, that caused a lot of errors, fixed the size issue by putting the Base_Item.h in the includes, thanks!