0

I am trying to create a TArray of my own data-type APlaying_Card. I get an error Cannot access private member in class APlaying_Card.

In the Unreal documentation for TArray it states:

A dynamically sized array of typed elements. Makes the assumption that your elements are relocate-able; 
i.e. that they can be transparently moved to new memory without a copy constructor. 
The main implication is that pointers to elements in the TArray may be invalidated by adding or removing other 
elements to the array. Removal of elements is O(N) and invalidates the indices of subsequent elements.

Caution: as noted below some methods are not safe for element types that require constructors.

There are two things I don't fully understand. First is the 'relocate-able' part, what does it mean to ensure it can be moved without a copy constructor and how is this done? Secondly, regarding the final statement in their remarks, How can I safely make an array of my own objects, if they do require a constructor?

As always thank you for your help.

I_Keep_Trying
  • 395
  • 3
  • 17
  • https://stackoverflow.com/questions/146452/what-are-pod-types-in-c is usually sufficient – Jeffrey Aug 28 '21 at 12:41
  • ok thanks for the help. I wonder how to make a dynamic collection of objects that do have constructors. Incidentally, I have discovered that I get the error only because I tried to declare my TArray from a class that is not a UCLASS. Since writing question, I made another class `Dealer` that is an actor, and this actor can declare the same array without the error. Progress is made little by little :D – I_Keep_Trying Aug 28 '21 at 12:54
  • `UObject`s must be allocated by ultimately calling `NewObject`, a `TArray` instead calls the standard `new` or `new[]` operator. The solution is to use an array of pointers instead `TArray` so `APlaying_Card` itself won't ever need to be reallocated. `UObject`s are also garbage collected so you won't ever need to worry about deleting them, although `AActor`s are an exception since they're referenced by the owning world. If this array is intended to be owning you'll need to remember to call DestroyActor when popping an element. – George Aug 28 '21 at 13:33

0 Answers0