Referencing a class inside a structure(present in that class)

Defining Struct Functions in the CPP of the releated UClass

Dear Envenger,

What I am saying holds true if you define a USTRUCT above the UClass that it is related to.

You can simply declare your struct function as normal in the .h, but instead of defining it as well, define it in the .cpp of your related class.


**ABaseInventoryItem.h**

**this**

//this is your USTRUCT function that wants to refer to the UCLASS below it


```

const bool SetStackAmount(int32 &NewStackAmount)
	{
		const ABaseInventoryItem* BaseItem = GetDefaultItem();
		if (BaseItem->ItemType == EItemType::StackableItem || BaseItem->ItemType == EItemType::StackableUsableItem)
		{}
}

```



**becomes this**



```

const bool SetStackAmount(int32 &NewStackAmount);

```



ABaseInventoryItem.cpp

Then in the .cpp you write this:

//Note the context is now your Struct name, not the Class


const bool FYourStructName::SetStackAmount(int32 &NewStackAmount)
	{
		const ABaseInventoryItem* BaseItem = GetDefaultItem();
		if (BaseItem->ItemType == EItemType::StackableItem || BaseItem->ItemType == EItemType::StackableUsableItem)
		{}
}


**Forward Declaration**

You could even forward declare your class name if you needed it in the function definition

.h


```

const bool SetStackAmount(class ABaseInventoryItem* Item);

```



and then define the rest in the .cpp of your ABaseInventoryItem.cpp

Why / How?

C++ just needs the .h declarations to all make sense, you can define stuff anywhere you want, including in separate .cpp files, as long as you include the relevant .h files.

Using forward declaration + defining UStruct functions in a .cpp you can more than do what you want.

:slight_smile:

Enjoy!

Rama