C++ dynamic 2D array to pointers?

Hi,

I’m trying to create a dynamic 2D array to pointers to objects (i.e. pointer to array of pointers to pointers)
The only way I thought I could approach this is through

type*** arrayName;

I’m getting the following error:
error : In BoardGrid: Missing '’ in Expected a pointer type*

Here’s a snippet of both the .h and .cpp code:

.h

UCLASS()
class MYBOARD_API ABoardGrid : public ABoardCell
{
	GENERATED_UCLASS_BODY()
	~ABoardGrid();

	UPROPERTY(VisibleDefaultsOnly, BlueprintReadOnly, Category = Board)
	TSubobjectPtr<USceneComponent> BoardDummyRoot;

	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Board)
	ABoardCell*** MainBoardGrid;

	UPROPERTY(EditAnywhere, Category = Board)
	TSubclassOf<ABoardCell> SpawnedCell;

	UPROPERTY(VisibleDefaultsOnly, BlueprintReadOnly, Category = Board)
	int32 mNumBoardRows;

	UPROPERTY(VisibleDefaultsOnly, BlueprintReadOnly, Category = Board)
	int32 mNumBoardColumns;

	UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = Board)
	float CellSpacing;

	virtual void BeginPlay() override;
	
};


.cpp

void ABoardGrid::BeginPlay()
{
	Super::BeginPlay();

	MainBoardGrid = new ABoardCell**[mNumBoardRows];

	for (int32 i = 0; i < mNumBoardRows; i++)
	{
		MainBoardGrid[i] = new ABoardCell*[mNumBoardColumns];

		for (int32 j = 0; j < mNumBoardColumns; j++)
		{
			if (SpawnedCell != NULL)
			{
				UWorld* const World = ();
				if (World)
				{
					FActorSpawnParameters SpawnParams;
					SpawnParams.Owner = this;
					SpawnParams.Instigator = Instigator;

					const float XOffset = j * CellSpacing;
					const float YOffset = i * CellSpacing;
					const FVector CellLocation = FVector(XOffset, YOffset, 0.f) + GetActorLocation();

					FRotator CellRotation;
					CellRotation.Yaw = 0.f;
					CellRotation.Pitch = 0.f;
					CellRotation.Roll = 0.f;

					MainBoardGrid[i][j] = World->SpawnActor<ABoardCell>(SpawnedCell, CellLocation, CellRotation, SpawnParams);
				}
			}
			else
				MainBoardGrid[i][j] = NULL;
		}

	}
}

Then in the destructor function I’m completely deleting all allocated memory…but looks like c++ is complaining about the ABoardCell*** MainBoardGrid; declaration.

ABoardCell is a class publicly inheriting from AActor

Is the declaration valid?
Does ()->SpawnActor<>() return a pointer or a copy of an object?

I’ve seen straight c++ implementations that uses the *** declaration. It’s a dynamic 2D table that holds pointers to objects.

Any help is appreciated

I think this is not C++ error but UHT error which may not know how to deal with pointer of pointer of pointer in UE4 refrections. Why dont you do this type insted?

TArray<TArray <ABoardCell*>>

But in UE3 unreal vm could not handle array in array so im not 100% sure if this gonna work in UE4

Thank you and sorry for the delayed reply…

so I tried:

TArray<TArray<ABoardCell*>> MainBoardGrid;

But I got the following error:
1>…/BoardGrid.h(21): error : In BoardGrid: Arrays within arrays not supported.

You could create a struct to hold the second array, so like the following:

struct FBoardCellChildren
{
	TArray<ABoardCell*> Children;
}

TArray<FBoardCellChildren> MainBoardGrid;

Then you could potentially override operator[] on the struct to access the array children directly. I have not tested this.

You have to use the method Ehamloptiran mentions, you’ll have to make a tarray of USTRUCTs that have tarrays themselves. Look for other USTRUCT examples for how to set it up. You could also do static array of USTRUCTS of static array if you want. This ends up making cleaner code anyway, any time you get more than 2 *'s it gets confusing fast.

Thank you Ehamloptiran, yes this worked. I ended up using 's 2D STRUCT array example found here:

link text

Thanks for the info. Yes, I used Ehamloptiran method also found in one of 's examples and it worked seamlessly.