# Operator Overloading

**URL:** <https://forums.unrealengine.com/t/operator-overloading/298784>\
**Category:** Programming & Scripting\
**Tags:** unreal-engine\
**Created:** [November 11, 2014, 10:24pm UTC](https://forums.unrealengine.com/t/operator-overloading/298784 "2014-11-11T22:24:41Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![anonymous\_user\_d168df591](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_d168df591](https://forums.unrealengine.com/u/anonymous_user_d168df591)\
**Post date:** [November 11, 2014, 10:24pm UTC](https://forums.unrealengine.com/t/operator-overloading/298784/1 "2014-11-11T22:24:41Z")

</div>

Hey Guys,

I’m having a slight bit of trouble implementing some simple operator overload (it’s for a short demo need example of topic). I currently have my code set up as follows:

Header:

```
friend FString& operator<<(FString &Str, const int32& Value);

```

Cpp:

```
FORCEINLINE	FString& operator<<(FString &Str, const int32& Value)
{
	Str += FString::FromInt(Value);
	return Str;
}

```

That builds fine for me but when I try use it in one of my other classes e.g:

```
FString Ammo; Ammo << MyCharacter->Ammo;

```

I’m getting error "no operator ‘\<\<’ matches these operands, types are FString \<\< int32 also if I try use the operator directly when creating FString it keeps saying a “;” is missing e.g:

```
FString Health << MyCharacter->Health;

```

Also I should note above I have code `FString& operator<<` this is from looking at the tutorial [here](https://wiki.unrealengine.com/Operator_Overloads) I’ve also tried this setup with `FString &operator<<` which is the way it is shown for operator overloading in the C++ notes I’m working out of, neither works for me.

---

<div class="post-metadata">

**Author:** ![ogrewizardninja\_1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@ogrewizardninja\_1](https://forums.unrealengine.com/u/ogrewizardninja_1)\
**Post date:** [November 12, 2014, 2:11am UTC](https://forums.unrealengine.com/t/operator-overloading/298784/2 "2014-11-12T02:11:20Z")

</div>

In which header have you added the declaration? And have you included that header in the part of your project where you are trying to use it?

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_d168df591](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_d168df591](https://forums.unrealengine.com/u/anonymous_user_d168df591)\
**Post date:** [November 12, 2014, 10:22am UTC](https://forums.unrealengine.com/t/operator-overloading/298784/3 "2014-11-12T10:22:54Z")

</div>

I have a .h and .cpp specifically for the operator over loading that’s where the first two snippets of code are respectively. And the .h for operator overloading is included in the .cpp file where I am trying to use the overloaded operator (third/fourth snippet).

---

<div class="post-metadata">

**Author:** ![Rama](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/rama/32/882413_2.png) [@Rama](https://forums.unrealengine.com/u/Rama)\
**Post date:** [November 12, 2014, 12:41pm UTC](https://forums.unrealengine.com/t/operator-overloading/298784/4 "2014-11-12T12:41:31Z")

</div>

#Solusion

You shouldn’t put operator overloads in .cpp files, just keep it in the .h

```
//some .h file that stores all your operators
FORCEINLINE FString& operator<<(FString &Str, const int32& Value)
 {
     Str += FString::FromInt(Value);
     return Str;
 }

```

and put in in a .h file that is included in every class that needs to use the operator

#UObject Static Function Library

An ideal place to put operators is in a .h file that is a static function library where you store all your core custom code, which is then included in all your major game class .h files.

#Wiki on Static Function Libraries

> **[A new, community-hosted Unreal Engine Wiki](https://forums.unrealengine.com/t/a-new-community-hosted-unreal-engine-wiki/141494)**
>
> After over a year in maintenance mode, the official Unreal Engine Wiki is now permanently offline. These resources now live on a new community-run Unreal Engine Community Wiki — ue4community.wiki! You will be able to find content from the official...

🙂

RAma
