x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

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);
 
 }


Product Version: UE 4.20
Tags:
more ▼

asked Sep 17 '18 at 04:11 AM in C++ Programming

avatar image

StarKiller4011
3 1 1

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

1 answer: sort voted first

Hi!

1) Delete static TArray bodies; from your .cpp

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

Compile.

more ▼

answered Sep 17 '18 at 10:16 AM

avatar image

Shiggi
147 6 7 9

avatar image StarKiller4011 Sep 17 '18 at 11:36 AM

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.

avatar image Shiggi Sep 17 '18 at 12:23 PM

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:alt text

example2.png (353.1 kB)
avatar image StarKiller4011 Sep 17 '18 at 12:57 PM

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.

avatar image Shiggi Sep 17 '18 at 01:01 PM

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)

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question