Declaration is incompatible with TArray

Hello everyone,

I’m new to both C++ and the Unreal Engine, but I decided to give learning it a shot.
I’ve encountered the error described when trying to modify the puzzle sample and return an array of blocks.

Z3BlockGrid.cpp

#include "Z3.h"
#include "Z3BlockGrid.h"
#include "Z3Block.h"
#include "Components/TextRenderComponent.h"

AZ3BlockGrid::AZ3BlockGrid(const FObjectInitializer& ObjectInitializer)
	: Super(ObjectInitializer)
{
	// Create dummy root scene component
	DummyRoot = ObjectInitializer.CreateDefaultSubobject<USceneComponent>(this, TEXT("Dummy0"));
	RootComponent = DummyRoot;

	// Create static mesh component
	ScoreText = ObjectInitializer.CreateDefaultSubobject<UTextRenderComponent>(this, TEXT("ScoreText0"));
	ScoreText->SetRelativeLocation(FVector(200.f, 0.f, 0.f));
	ScoreText->SetRelativeRotation(FRotator(90.f, 0.f, 0.f));
	ScoreText->SetText(TEXT("Score: 0"));
	ScoreText->AttachTo(DummyRoot);

	// Set defaults
	Size = 6;
	BlockSpacing = 250.f;
}


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

	// Number of blocks
	const int32 NumBlocks = Size * Size;

	//int** ary = new int*[xMax];
	//for (int i = 0; i < xMax; ++i)
	//ary[i] = new int[yMax];

	// 2D array for mapping each block to coordinates
	//int xMax = ((NumBlocks - 1) / Size);
	//int yMax = ((NumBlocks - 1) % Size);

	//AZ3Block tilemap [5];

	// second method:
	//std::vector<Car> mycars; // empty
	//mycars.reserve(userInput); // optional: reserve the memory upfront

	//TArray<AZ3Block*> tilemap;
	Tilemap.Reserve(NumBlocks);

	// Loop to spawn each block
	for (int32 BlockIndex = 0; BlockIndex < NumBlocks; BlockIndex++)
	{
		int xCoord = (BlockIndex / Size);
		int yCoord = (BlockIndex % Size);

		const float XOffset = (BlockIndex / Size) * BlockSpacing; // Divide by dimension
		const float YOffset = (BlockIndex % Size) * BlockSpacing; // Modulo gives remainder

		// Make postion vector, offset from Grid location
		const FVector BlockLocation = FVector(XOffset, YOffset, 0.f) + GetActorLocation();

		// Spawn a block
		AZ3Block* NewBlock = GetWorld()->SpawnActor<AZ3Block>(BlockLocation, FRotator(0, 0, 0));
		NewBlock->xPos = xCoord;
		NewBlock->yPos = yCoord;

		// Tell the block about its owner
		if (NewBlock != NULL)
		{
			NewBlock->OwningGrid = this;
		}

		// Add the spawned blocks to the array
		Tilemap.Insert(NewBlock, xCoord * yCoord + yCoord);
	}
}


void AZ3BlockGrid::AddScore()
{
	// Increment score
	Score++;

	// Update text
	FString ScoreString = FString::Printf(TEXT("Score: %d"), Score);
	ScoreText->SetText(ScoreString);
}

TArray<AZ3Block*> AZ3BlockGrid::GetTiles() 
{
	return Tilemap;
}

Sorry for that wall of code. The issue is with that very last function. It keeps giving me an error that "declaration is incompatible with TArray

Maybe you can try…

TArray<AZ3Block> * AZ3BlockGrid::GetTiles()

can you please post the log of the compilation? and what is the type of Tilemap?

Thanks for the suggestion, but that didn’t do anything. I’m a little confused as to how this could’ve helped in the first place.

I’ve not tried to compile yet, because I can’t solve this error which the IDE catches. And Tilemap has type TArray.

“It could also be due to not including “Z3Block.h” in Z3BlockGrid.h, if that’s the case then include it there and remove it from this cpp file here. Again i’m just guessing things cause i really can’t see the whole thing.”

This was why. I’d actually included it, but in the wrong order. Thanks for the suggestion!

I’m not really sure dude since i don’t see the header file as well there could be a problem there. Did you make sure that the declaration of the function GetTiles returns that same type TArray ?

It could also be due to not including “Z3Block.h” in Z3BlockGrid.h, if that’s the case then include it there and remove it from this cpp file here. Again i’m just guessing things cause i really can’t see the whole thing.

Well… I thought that since your trouble was with “declaration” maybe change the return to a “pointer to an array” instead of “an array of pointers” should bypass some weird compiler error.

Anyway… Sorry.

Thanks for clarifying! Like I said, I’m new to C++ and didn’t think that could genuinely have been a problem. Don’t feel sorry for trying to help. :slight_smile: