Why can't I set a pointer to my struct?

I maintain a global list of my character data struct in my world manager class, and I’m trying to configure individual AMyCharacter actors to maintain a reference to their data:

//AMyCharacter.cpp:
FCharacterSheet* characterSheet;

void AMyCharacter::BeginPlay()
{
	Super::BeginPlay();
	characterSheet = &worldManager->characterList[0];
}

//AWorldManager.h:
TArray<FCharacterSheet> characterList;

The line where I declare characterSheet has an error, “Inappropriate ‘*’ on variable of type ‘FCharacterSheet’, cannot have an exposed pointer to this type”. Does this mean that I’m declaring my struct wrong, or does the struct itself need some kind of special formatting to allow pointers?

Related Question with answer:

Oh hey, thanks for the citation. :slight_smile:

That covers the definition, but when I’m trying to make this a variable to be set later (instead of a function argument), I get a different error, to wit “reference must be initialized”. If I’m treating this like a pointer, shouldn’t it auto-initialize to nullptr?

UPROPERTY(BlueprintReadOnly)
FCharacterSheet& characterSheet;

C++ requires references to be initialized. What would an uninitialized reference be? They aren’t pointers, it would be some object in an invalid state, but what would it be referencing, the best a compiler could do for you is make some stack space and call the default constructor on the uninitialized reference, but that is pretty undefined behavior, and better to throw a compiler error than let that get through. This is a language requirement in C++.

1 Like