# How to overload operator == for (TArray)?

**URL:** <https://forums.unrealengine.com/t/how-to-overload-operator-for-tarray/633044>\
**Category:** C++\
**Tags:** UE4-20, unreal-engine, UE5-0, UE4, question, CPP\
**Created:** [August 24, 2022, 2:47am UTC](https://forums.unrealengine.com/t/how-to-overload-operator-for-tarray/633044 "2022-08-24T02:47:23Z")\
**Posts on this page:** 20\
**Page:** 2

<div class="post-metadata">

**Author:** ![GigiK-K-K](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@GigiK-K-K](https://forums.unrealengine.com/u/GigiK-K-K)\
**Post date:** [August 24, 2022, 8:15am UTC](https://forums.unrealengine.com/t/how-to-overload-operator-for-tarray/633044/21 "2022-08-24T08:15:37Z")

</div>

It’s not a function created by you that you are returning, so that is like one problem with the types, second problem is it you are overloading it ? as I can see that parameter is only one inside the function and you got two params on the function call.

It only has one parameter in the form of a pointer with it’s type named item

```auto
SizeType Find(const ElementType& Item) const
	

```

So maybe when you call array find you are calling more than one function and this is just one of them ? otherwise you can’t fit two parameters on a call to a function that only has to offer one.

Is this created by you ?

````auto
SizeType Find(const ElementType& Item) const
	{
		const ElementType* RESTRICT Start = GetData();
		for (const ElementType* RESTRICT Data = Start, *RESTRICT DataEnd = Data + ArrayNum; Data != DataEnd; ++Data)
		{
			if (*Data == Item)
			{
				return static_cast<SizeType>(Data - Start);
			}
		}
		return INDEX_NONE;
	}```

````

---

<div class="post-metadata">

**Author:** ![Alexa.Ki](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/alexa.ki/32/562753_2.png) [@Alexa.Ki](https://forums.unrealengine.com/u/Alexa.Ki)\
**Post date:** [August 24, 2022, 8:37am UTC](https://forums.unrealengine.com/t/how-to-overload-operator-for-tarray/633044/22 "2022-08-24T08:37:09Z")

</div>

No, this is the part of the `Array_Find` function

```auto
template<typename T, typename U>
    static int32 Array_Find(const TArray<T>& TargetArray, const U& ItemToFind)
    {
        return TargetArray.Find(ItemToFind);
    }

```

---

<div class="post-metadata">

**Author:** ![GigiK-K-K](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@GigiK-K-K](https://forums.unrealengine.com/u/GigiK-K-K)\
**Post date:** [August 24, 2022, 8:43am UTC](https://forums.unrealengine.com/t/how-to-overload-operator-for-tarray/633044/23 "2022-08-24T08:43:32Z")

</div>

It has two parameters so array find calls on multiple locations one the find function then a Template array, okay, template meaning for template, it can be any kind ?

So that is the first parameter and it’s calling not on the find function as I see, Location is calling on it. You gave wrong function.

```auto
First parameter is 
TransformArray
Second is Location

```

The Array\_find calls on multiple locations not just in one place

You made a Ftransfrom parameter for:

```auto
TArray<T>& TargetArray

```

and another parameter named Location for the function you posted.

```auto
const U& ItemToFind

```

So you induced us in error 🙂

Actuall the last parameter is for the function

```auto
const U& ItemToFind

```

```auto
const TArray<T>& TargetArray,

```

This has nothing to do with the function posted by you, it may be valid for your Ftransform because it’s a Template array I take it the stands for any type of template and since FTransfrom is a template part of the Tarray is aceptable.

So most probally it’s the INT.

You are tryig to say that the template array parameter that now is of Ftransform is to be stored inside an INT variable called index.

So things have cleared up regarding your Ftransfrom, it was a parameter for \< T \> type “teplate type” and it is the first parameter as I see the function is detailed in the unreal docs now. The second parameter “Location” is for that function, only the second one.

---

<div class="post-metadata">

**Author:** ![Alexa.Ki](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/alexa.ki/32/562753_2.png) [@Alexa.Ki](https://forums.unrealengine.com/u/Alexa.Ki)\
**Post date:** [August 24, 2022, 8:54am UTC](https://forums.unrealengine.com/t/how-to-overload-operator-for-tarray/633044/24 "2022-08-24T08:54:04Z")

</div>

I understand and thank You for the detailed information , really learned new things from your answer 🙂

---

<div class="post-metadata">

**Author:** ![GigiK-K-K](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@GigiK-K-K](https://forums.unrealengine.com/u/GigiK-K-K)\
**Post date:** [August 24, 2022, 8:55am UTC](https://forums.unrealengine.com/t/how-to-overload-operator-for-tarray/633044/25 "2022-08-24T08:55:46Z")

</div>

Problem is with INT.  
Just for the fun of it, make an Ftransform instead of INT.

Sort of

```auto
FTransfrom somevar;

somevar = FCustomThunkTemplates::Array_Find(TransformArray, Location);

```

If no error is shown then it is in fact the INT, I don’t see any form of conversion in your code.

Why are you bent on using Ftransfrom, use vectors dear.

---

<div class="post-metadata">

**Author:** ![Alexa.Ki](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/alexa.ki/32/562753_2.png) [@Alexa.Ki](https://forums.unrealengine.com/u/Alexa.Ki)\
**Post date:** [August 24, 2022, 8:57am UTC](https://forums.unrealengine.com/t/how-to-overload-operator-for-tarray/633044/26 "2022-08-24T08:57:22Z")

</div>

> [@c0r37py](#):
>
> myclass.cpp

than You for correcting me, it works perfectly but I got some empty locations, do you hve any idea what is this?

---

<div class="post-metadata">

**Author:** ![GigiK-K-K](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@GigiK-K-K](https://forums.unrealengine.com/u/GigiK-K-K)\
**Post date:** [August 24, 2022, 8:57am UTC](https://forums.unrealengine.com/t/how-to-overload-operator-for-tarray/633044/27 "2022-08-24T08:57:51Z")

</div>

It can also be that the universal \< T \> array teplate is not accepting your Ftransform for some reason and it says that it’s a conversion error. Maybe you should check how to set types on the array, or what types does \< T \> accept

---

<div class="post-metadata">

**Author:** ![Alexa.Ki](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/alexa.ki/32/562753_2.png) [@Alexa.Ki](https://forums.unrealengine.com/u/Alexa.Ki)\
**Post date:** [August 24, 2022, 9:01am UTC](https://forums.unrealengine.com/t/how-to-overload-operator-for-tarray/633044/28 "2022-08-24T09:01:40Z")

</div>

I am bent using transform becasue vector will not help me in case I am spawning the actors in the world space transforms of the level using `GetComponentTransforms`

---

<div class="post-metadata">

**Author:** ![Alexa.Ki](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/alexa.ki/32/562753_2.png) [@Alexa.Ki](https://forums.unrealengine.com/u/Alexa.Ki)\
**Post date:** [August 24, 2022, 9:04am UTC](https://forums.unrealengine.com/t/how-to-overload-operator-for-tarray/633044/29 "2022-08-24T09:04:28Z")

</div>

Sir I think `GetComponentTransform` is only available for `FTransform`

 ![image_2022-08-24_140331945](https://d3kjluh73b9h9o.cloudfront.net/original/4X/3/1/a/31ae81202d228a4784e45a0661e78fcb5ded4e2a.png)

---

<div class="post-metadata">

**Author:** ![GigiK-K-K](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@GigiK-K-K](https://forums.unrealengine.com/u/GigiK-K-K)\
**Post date:** [August 24, 2022, 9:07am UTC](https://forums.unrealengine.com/t/how-to-overload-operator-for-tarray/633044/30 "2022-08-24T09:07:11Z")

</div>

> [@Alexa.Ki](#):
>
> `TArray<T>`

I’m not sure how this works \< T \> , I’m not familiar with it, I never used it.  
But that might be the problem also, as it is not recondising your Ftransform and thinks you want to convert from it to \< T \> and it may not find the type of Ftransform compatible with it.

---

<div class="post-metadata">

**Author:** ![GigiK-K-K](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@GigiK-K-K](https://forums.unrealengine.com/u/GigiK-K-K)\
**Post date:** [August 24, 2022, 9:07am UTC](https://forums.unrealengine.com/t/how-to-overload-operator-for-tarray/633044/31 "2022-08-24T09:07:42Z")

</div>

> I am bent using transform becasue vector will not help me in case I am spawning the actors in the world space transforms of the level using `GetComponentTransforms`

You can do that with vectors with functions.

---

<div class="post-metadata">

**Author:** ![c0r37py](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/c0r37py/32/559700_2.png) [@c0r37py](https://forums.unrealengine.com/u/c0r37py)\
**Post date:** [August 24, 2022, 7:44pm UTC](https://forums.unrealengine.com/t/how-to-overload-operator-for-tarray/633044/32 "2022-08-24T19:44:44Z")

</div>

can you show a snapshot of the printed locations? I have no idea what locatoins are you talking about

---

<div class="post-metadata">

**Author:** ![Alexa.Ki](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/alexa.ki/32/562753_2.png) [@Alexa.Ki](https://forums.unrealengine.com/u/Alexa.Ki)\
**Post date:** [August 24, 2022, 7:46pm UTC](https://forums.unrealengine.com/t/how-to-overload-operator-for-tarray/633044/33 "2022-08-24T19:46:01Z")

</div>

![l17](https://d3kjluh73b9h9o.cloudfront.net/original/4X/c/f/b/cfbdf216befe42794d5bd2833babfac9085de306.png)  
i mean duplicates location

---

<div class="post-metadata">

**Author:** ![c0r37py](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/c0r37py/32/559700_2.png) [@c0r37py](https://forums.unrealengine.com/u/c0r37py)\
**Post date:** [August 24, 2022, 7:47pm UTC](https://forums.unrealengine.com/t/how-to-overload-operator-for-tarray/633044/34 "2022-08-24T19:47:33Z")

</div>

floats are bolted values and use `nearlyequal` instead of `==`

hope it helps  
cheers!

---

<div class="post-metadata">

**Author:** ![Alexa.Ki](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/alexa.ki/32/562753_2.png) [@Alexa.Ki](https://forums.unrealengine.com/u/Alexa.Ki)\
**Post date:** [August 24, 2022, 7:49pm UTC](https://forums.unrealengine.com/t/how-to-overload-operator-for-tarray/633044/35 "2022-08-24T19:49:51Z")

</div>

how to use `nearlyequal` under `ContainsByPredicate` ?

```auto
do
	{
		RandomLocation(Available, Location);
	} while (UsedLocations.ContainsByPredicate([Location](const FTransform Transform)
	{
		return Transform.GetLocation() nearlyequal Location.GetLocation()
			&& Transform.GetRotation() nearlyequal Location.GetRotation()
			&& Transform.GetScale3D() nearlyequal Location.GetScale3D();
	}));
	SpawnLocation = Location;
	UsedLocations.Add(SpawnLocation);

```

this doesn’t compiles 😫

---

<div class="post-metadata">

**Author:** ![c0r37py](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/c0r37py/32/559700_2.png) [@c0r37py](https://forums.unrealengine.com/u/c0r37py)\
**Post date:** [August 24, 2022, 7:52pm UTC](https://forums.unrealengine.com/t/how-to-overload-operator-for-tarray/633044/36 "2022-08-24T19:52:32Z")

</div>

🤣`nearlyequal `  
test it and show the results

```auto
return Transform.GetLocation().Equals(Location.GetLocation(), KINDA_SMALL_NUMBER);

```

hope it helps,  
cheers!

---

<div class="post-metadata">

**Author:** ![Alexa.Ki](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/alexa.ki/32/562753_2.png) [@Alexa.Ki](https://forums.unrealengine.com/u/Alexa.Ki)\
**Post date:** [August 24, 2022, 8:49pm UTC](https://forums.unrealengine.com/t/how-to-overload-operator-for-tarray/633044/37 "2022-08-24T20:49:44Z")

</div>

still got few locations duplicated

---

<div class="post-metadata">

**Author:** ![GigiK-K-K](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@GigiK-K-K](https://forums.unrealengine.com/u/GigiK-K-K)\
**Post date:** [August 25, 2022, 6:05am UTC](https://forums.unrealengine.com/t/how-to-overload-operator-for-tarray/633044/38 "2022-08-25T06:05:37Z")

</div>

Solved your problem ?  
Now that I’m rested 🙂 I can see that var location is also a Ftransfrom.  
So as I said no Ftransform in that function, Location parameter is suppose to input a value for the function return that you have there that returns parameter “Item” I believe that is not of Ftransform Type.  
ElementType is the type being return there with the name "Item:  
Maybe element type accepts all things maybe not Ftransform, or maybe also Ftransform.

I got a page here

> **[operator==](https://docs.unrealengine.com/5.0/en-US/API/Functions/op_cmp_eq/)**
>
> Overload list

From the page  
[  
operator==  
 ](https://docs.unrealengine.com/5.0/en-US/API/Runtime/Core/Containers/op_cmp_eq/1)

(  
[TArrayView](https://docs.unrealengine.com/5.0/en-US/API/Runtime/Core/Containers/TArrayView)\< ElementType, SizeType \>,  
[TArrayView](https://docs.unrealengine.com/5.0/en-US/API/Runtime/Core/Containers/TArrayView)\< OtherElementType, Other…  
)  
So if you are trying to match things this should be a good resource.  
Why is it giving “==” error , I don’t see this operator in your code now posted by you  
I understand the conversion error tho on the Ftransform where you are trying to fit Ftranssform into universal types and maybe they don’t like Ftransform.

---

<div class="post-metadata">

**Author:** ![Alexa.Ki](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/alexa.ki/32/562753_2.png) [@Alexa.Ki](https://forums.unrealengine.com/u/Alexa.Ki)\
**Post date:** [August 25, 2022, 6:59am UTC](https://forums.unrealengine.com/t/how-to-overload-operator-for-tarray/633044/39 "2022-08-25T06:59:15Z")

</div>

> [@Alexa.Ki](#):
>
> `Index = FCustomThunkTemplates::Array_Find(TransformArray, Location);`

yes this is confusing, we see the code without `==` but internally I am sure some comparison is happening because this is returning the `index` of the `FTransform` in the `Ftransform Array`

```auto
Index = FCustomThunkTemplates::Array_Find(TransformArray, Location);

```

example:

```auto
FindIndex = FCustomThunkTemplates::Array_Find(Available, Location);
bEqualValid = UKismetMathLibrary::EqualEqual_IntInt(FindIndex, -1);

```

## i updated the question, thank you for correcting me )

---

<div class="post-metadata">

**Author:** ![GigiK-K-K](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@GigiK-K-K](https://forums.unrealengine.com/u/GigiK-K-K)\
**Post date:** [August 25, 2022, 7:11am UTC](https://forums.unrealengine.com/t/how-to-overload-operator-for-tarray/633044/40 "2022-08-25T07:11:36Z")

</div>

> [@GigiK-K-K](#):
>
> ElementType

Wait wait

```auto
 UKismetMathLibrary::EqualEqual_IntInt

```

UKismetMathLibrary converts things, I use this lib often.  
I don’t see UKismetMathLibrary in your code.

It’s obvious, at the end it has " `FindIndex` So the error is there when it’s trying to compare FindIndex with this library.

> **[UKismetMathLibrary::EqualEqual\_IntInt](https://docs.unrealengine.com/5.0/en-US/API/Runtime/Engine/Kismet/UKismetMathLibrary/EqualEqual_IntInt/)**
>
> Returns true if A is equal to B (A == B)

So it’s obvious that your Ftransform is not being converted to “int” as stated by me it’s the INT not converted. So Find Index is not an int, tho you made it an INT.  
The error happens at the library level where it’s trying to equal int to int, it finds that Ftransform is not an INT and gives you the error

> no operator found which takes a left-hand operand of type ‘const FTransform’

[Previous page](https://forums.unrealengine.com/t/how-to-overload-operator-for-tarray/633044.md?page=1)

[Next page](https://forums.unrealengine.com/t/how-to-overload-operator-for-tarray/633044.md?page=3)
