x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

Operator Overloading

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 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.

Product Version: Not Selected
Tags:
more ▼

asked Nov 11 '14 at 10:24 PM in C++ Programming

avatar image

Immoschu
1 1 3 5

avatar image ogrewizardninja Nov 12 '14 at 02:11 AM

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?

avatar image Immoschu Nov 12 '14 at 10:22 AM

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).

(comments are locked)
10|2000 characters needed characters left

1 answer: sort voted first

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

ttps://wiki.unrealengine.com/Static_Function_Libraries,_Your_Own_Version_of_UE4_C%2B%2B,_No_Engine_Compile_Times

:)

RAma

more ▼

answered Nov 12 '14 at 12:41 PM

avatar image

Rama
10.5k 444 343 1081

(comments are locked)
10|2000 characters needed characters left
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question