Download

C++ Unreal Engine 4 ERROR C4458. Please help.

Hello. I am working on making an Escape Room game in Unreal Engine 4 using C++. I’m pretty new to Unreal and C++ so I’m not really sure what to fix. I’m currently working on setting up an inventory system but have run into error C4458. Could anyone help me figure out why it’s saying this and how I could fix it?

The error: Character_Controller.h (45): error C4458: declaration of ‘item’ hides class member.
Character_Controller.h (34): note: see declaration of ‘FInventoryItem::item’

This is the code:

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

#pragma once

#include "CoreMinimal.h"
#include "Components/ActorComponent.h"
#include "GameFramework/Actor.h"
#include "GameFramework/Character.h"
#include "Components/InputComponent.h"
#include "Engine/DataTable.h"
#include "Engine/World.h"
#include "EscapeRoomGameModeBase.h"
#include "Item.h"
#include "Components/SphereComponent.h"
#include "Character_Controller.generated.h"

USTRUCT(BlueprintType)
struct FInventoryItem: public FTableRowBase
{
	GENERATED_BODY()

public:

	FInventoryItem()
	{
		name = FText::FromString("item");
		isVisible = false;
	}

	UPROPERTY(EditAnywhere, BlueprintReadWrite)
		FName itemId;

	UPROPERTY(EditAnywhere, BlueprintReadWrite)
		TSubclassOf<class AItem> item;

	UPROPERTY(EditAnywhere, BlueprintReadWrite)
		FText name;

	UPROPERTY(EditAnywhere, BlueprintReadWrite)
		bool isVisible;

	UPROPERTY(EditAnywhere, BlueprintReadWrite)
		UTexture2D* image;

	bool operator==(const FInventoryItem& item) const <----- ERROR HERE
	{
		if (itemId == item.itemId)
			return true;
		else return false;
	}
};

UCLASS()
class ESCAPEROOM_API ACharacter_Controller : public ACharacter
{
	GENERATED_BODY()

public:
	// Sets default values for this character's properties
	ACharacter_Controller();

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

	UFUNCTION(BlueprintCallable, Category = "Items")
		void Collect();

	UFUNCTION()
		void InventoryPlus();

	UFUNCTION()
		void InventoryMinus();

public:	
	// Called every frame
	virtual void Tick(float DeltaTime) override;

	// Called to bind functionality to input
	virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
	void Horizontal_Movement(float axis);
	void Vertical_Movement(float axis);

	UFUNCTION(BlueprintCallable, Category = "Utilities")
		void AddToInventory(FName id);

	UFUNCTION(BlueprintCallable, Category = "Utilities")
		void RemoveFromInventory();

	UFUNCTION()
		void Wielding();

	UPROPERTY(VisibleAnywhere, BlueprintReadWrite)
		TArray<FInventoryItem> inventory;

	UPROPERTY(EditAnywhere, BlueprintReadWrite)
		int inventoryI;

	UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = Camera, meta = (AllowPrivateAccess = "true"))
		class USphereComponent* collectionRange;

	UPROPERTY(EditAnywhere, BlueprintReadWrite)
		FInventoryItem wield;

	UPROPERTY(EditAnywhere, BlueprintReadWrite)
		FInventoryItem empty;

	UPROPERTY(EditAnywhere, BlueprintReadWrite)
		TArray<FInventoryItem> pickupableObjects;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Wield Items")
		TArray<AActor*> wieldObjects;

	FORCEINLINE class USphereComponent* GetCollectionRange() const { return collectionRange; }


};

Hi!

you have a class member named “item”:

	UPROPERTY(EditAnywhere, BlueprintReadWrite)
		TSubclassOf<class AItem> item;

and you also have a function parameter named “item”:

	bool operator==(const FInventoryItem& item) const <----- ERROR HERE
	{
		if (itemId == item.itemId)
			return true;
		else return false;
	}

You code is actually correct and the error you are getting is technically not an error but a warning. However, it is good practice to have your code compile without warnings so Unreal has the compiler configured to treat warnings as errors so you are forced to deal with them. The warning is that the “item” variable you use as function parameter hides the class member “item” because it has the same name. Just name the function parameter something else like “otherItem”. It is also common for overloaded comparison operators to simply call the identifier on the right “rhs”.

Thank you so much this is so helpful! I really appreciate this. I will change that :smiley: