Why is my struct still undefined?

Ok, I am trying to use a USTRUCT in UE4 but after following the documentation and looking at example after example of structs all over the Internet I am still left with the same compile error.

In Header file
#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "MyInvIconActor.generated.h"


UCLASS()
class SURVIVAL_PROTO1_API AMyInvIconActor : public AActor
{
	GENERATED_BODY()
	
	FIconDataStruct myIconData;
public:	
	// Sets default values for this actor's properties
	AMyInvIconActor();

protected:
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;

public:	
	// Called every frame
	virtual void Tick(float DeltaTime) override;

	int ID;
	int Amount;

	
};

USTRUCT()
struct FIconDataStruct {

	GENERATED_BODY()

public:
	UPROPERTY()
		int ID;

	FIconDataStruct() {
		ID = 0;
	}
};

In cpp file

#include "MyInvIconActor.h"

// Sets default values
AMyInvIconActor::AMyInvIconActor()
{
 	// Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;
	
}

// Called when the game starts or when spawned
void AMyInvIconActor::BeginPlay()
{
	Super::BeginPlay();

	myIconData.ID = 5;
}

// Called every frame
void AMyInvIconActor::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);

}

Error-
C:\Users\james\Documents\Unreal Projects\Survival_Proto1\Source\Survival_Proto1/MyInvIconActor.h(15) : error C2079: ‘AMyInvIconActor::myIconData’ uses undefined struct ‘FIconDataStruct’

I don’t understand what is wrong with my code. If the struct is undefined then I don’t see how it would let me do FIconDataStruct myIconData under generated body, the intellesense shows the struct which doesn’t usually happen if something is undefined because if it is undefined then in VS’s eyes it does not exist, So how is the intellesense suggesting it to me if it is undefined??? and it also lets me access the struct in the cpp file. How is this undefined???

You’ve declared the variable, but you haven’t set the value anywhere, so it has an undefined value.

You can set a default value in your constructor.

 // Sets default values
 AMyInvIconActor::AMyInvIconActor()
  :
  myIconData(FIconDataStruct())// here you can call the default constructor of your struct, and set the default value of your myIconData variable
 {
      // Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
     PrimaryActorTick.bCanEverTick = true;
     
 }

You should also verify you are including the header file that defines the FIconDataStruct.

FIconDataStruct is used by AMyInvIconActor but not defined until after that point. C++ requires types to be defined before they’re used, so in this case you should define the struct before the class.

1 Like

Yes That is what the problem was. I had tried that earlier before posting the question because I did not realize you can’t trust VS intel when it comes to UE4. Lots of red lines appeared when moving the struct above the class so I didn’t even try and compile it LOL. Thanks for your Answer.