Error: expression must have pointer type

I’m using UE 4.15.2

The ‘Barrel’ in the TankAimingComponent is a pointer.

void UTankAimingComponent::AimAt(FVector HitLocation)
{
auto OurTankName = GetOwner()->GetName();
auto BarrelLocation = Barrel->GetComponentLocation()->ToString();
    ...

Barrel-> Should work, but I’m getting an error: “expression must have pointer type”

I’m lost on this one. Why is this complaining that it needs a pointer??? It IS a pointer…

In fact, you can call other member functions of that UStaticMeshComponent in the same manner…

Can you include the code where you created Barrel, probably in the .h file. Also if you are getting the barrel’s location in order to put it in a

UE_LOG
remember that Unreal requires you to de-reference the the BarrelLocation return value.
    auto BarrelLocation = Barrel->GetComponentLocation()->ToString();
    UE_LOG(LogTemp, Warning, TEXT(" The barrel location is: %s), *BarrelLocation);

I don’t have issue with dereferencing the variable to get the value. I can’t even compile to make the call in the first place - it’s complaining in the IDE before compile. The Barrel-> is the piece the IDE is complaining about just for that function only. I can call all other Class functions from StaticMeshComponent in the same manner. I’ll paste the code you want in a sec - I’m afk.

Could you post your header and cpp file?

The really weird part is that the compiler thinks Barrel is an FVector…

The very first error is:

expression must have pointer type

then

error C2819: type ‘FVector’ does not have an overloaded member ‘operator ->’

ok - header for TankAimingComponent (.h)

#pragma once

#include "Components/ActorComponent.h"
#include "TankAimingComponent.generated.h"


UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
class BATTLETANK_API UTankAimingComponent : public UActorComponent
{
    GENERATED_BODY()

public:	
	// Sets default values for this component's properties
	UTankAimingComponent();

	void AimAt(FVector HitLocation);


	void SetBarrelReference(UStaticMeshComponent* BarrelToSet);

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

public:	
	// Called every frame
	virtual void TickComponent(float DeltaTime, ELevelTick TickType, 
FActorComponentTickFunction* ThisTickFunction) override;

private: 
	UStaticMeshComponent* Barrel = nullptr;
};

and now the CPP file:

// Fill out your copyright notice in the Description page of Project Settings.

#include "BattleTank.h"
#include "TankAimingComponent.h"


// Sets default values for this component's properties
UTankAimingComponent::UTankAimingComponent()
{
	// Set this component to be initialized when the game starts, and to be ticked every frame.  You can turn these features
	// off to improve performance if you don't need them.
	PrimaryComponentTick.bCanEverTick = true;

	// ...
}

void UTankAimingComponent::SetBarrelReference(UStaticMeshComponent * BarrelToSet)
{
	Barrel = BarrelToSet;
}

// Called when the game starts
void UTankAimingComponent::BeginPlay()
{
	Super::BeginPlay();

	// ...

}

// Called every frame
void UTankAimingComponent::TickComponent(float DeltaTime, ELevelTick TickType, 
FActorComponentTickFunction* ThisTickFunction)
{
	Super::TickComponent(DeltaTime, TickType, ThisTickFunction);

	// ...
}

void UTankAimingComponent::AimAt(FVector HitLocation)
{
	auto OurTankName = GetOwner()->GetName();
	auto BarrelLocation = Barrel->GetComponentLocation()->ToString();
	UE_LOG(LogTemp, Warning, TEXT("Tank %s aiming at: %s from %s"), *OurTankName, 
*HitLocation.ToString(), *BarrelLocation);
}

Now look, even intellisense “knows” it’s a pointer… !!!

I FOUND THE PROBLEM

oh boy…you’ll love it…

auto BarrelLocation = Barrel->GetComponentLocation()->ToString();

The first error was expression must have pointer type
The second error was type FVector does not have an overloaded member ‘operator’

Now the ‘misleading part’ - the underline for the error is under the ‘Barrel’ variable…

It’s not a problem with the ‘Barrel’ member variable!

The problem is with _the return value for GetComponentLocation()

So the correct code should be:

auto BarrelLocation = Barrel->GetComponentLocation().ToString();

(notice the correct code is:

GetComponentLocation().ToString();

and NOT

GetComponentLocation()->ToString();

and voila… it compiles…

sheesh

Privacy & Terms