Static member TArray as list of all instances

I am trying to use a static TArray as a list of all instances of my class so that they are all aware of each other however I am getting the following error: no instance of overloaded function matches the argument list

here is my header:

UCLASS()
class ORBITALMECHANICSTEST_API AStellarBody : public AActor {
	GENERATED_BODY()

public:
	// Sets default values for this actor's properties
	AStellarBody();
	// Called every frame
	virtual void Tick(float DeltaTime) override;

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

protected:
	static TArray<AStellarBody> bodies;

};

and here is my cpp file:

#include "StellarBody.h"

static TArray<AStellarBody> bodies;

// Sets default values
AStellarBody::AStellarBody() {
	// 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;
	bodies.Add(this);
}

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

}

void AStellarBody::BeginDestroy() {
    bodies.Remove(this);
}

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

}

Hi!

  1. Delete static TArray bodies; from your .cpp

  2. Add to this line (3 in cpp) TArray AStellarBody::bodies;

Compile.

So I added removed the static keyword like you said, resulting in TArray<AStellarBody> AStellarBody::bodies; being added instead but I still get the same overload error. Also since TArray is templated I cannot remove the <AStellarBody>. Also it might help to know that making the TArray non-static in the header file removes the error. This is not a fix though as I need the TArray to be static so all instances of AStellarBody can know about every other instance.

Hmmm, in my case I didn’t have this issue. So I created a new blank project and this is my worked code:

Header:
#pragma once

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

UCLASS()
class MYPROJECT2_API ATextExample : public AActor
{
	GENERATED_BODY()
	
public:	
	// Sets default values for this actor's properties
	ATextExample();

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

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

protected:
	static TArray<ATextExample*> ExampleArray;
	
};

cpp:
#include “TextExample.h”

TArray<ATextExample*> ATextExample::ExampleArray;

// Sets default values
ATextExample::ATextExample()
{
 	// 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;
	ExampleArray.Add(this);
}

// Called when the game starts or when spawned
void ATextExample::BeginPlay()
{
	Super::BeginPlay();
	//ExampleArray.Add(this);
	for (int i = 0; i < ExampleArray.Num(); i++)
	{
		ATextExample* TestRef = ExampleArray[i];
		if (TestRef)
		{
			UE_LOG(LogTemp, Warning, TEXT("/TextExample.ccp/ array - %s"), *TestRef->GetName());
		}
	}
	
}

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

}

And I think more right place adding to array in Begin play.
Log here:

I see now that this entire situation is a result of me overlooking something stupid and small. I realized after having a second look that my TArray<AStellarBody> was requiring the objects themselves and not pointers like what this returns so leaving everything exactly as it was and only changine the template arg fixed everything for me. Thank you.

TLDR: I was being an idiot did

static TArray<AStellarBody> bodies;

instead of the correct:

static TArray<AStellarBody*> bodies;

The key difference there being the * turning the template arg into pointers and not values.

Hm, if I didn’t place pointer I got compile error every time) That’s why I didn’t find it. So, yea. This is a problem in your code)