How do I overolad operator+ for FString and numbers?

inline FString operator+(const FString &num1, const double &num2)
{
return num1 + std::to_string(num2).c_str();
}
inline FString operator+(const double &num1, const FString &num2 )
{
FString value = std::to_string(num1).c_str() + num2;

	return value;
}

This does not work, says:
Error 1 error C2111: ‘+’ : pointer addition requires integral operand

I am doing the call:

FJsonValue *jsonValue= bla bla;
double doubleNumber = jsonValue->AsNumber();
Logger::log("Number " + doubleNumber + "~~~" );
void Logger::log(const FString &str)
{
	FILE *f = fopen(Logger::fileName.c_str(), "at+");

	fprintf(f, "%s\n", TCHAR_TO_ANSI(*str));

	fclose(f);
}

Your second operator is calling c_str() which returns a const char* and then trying to perform pointer arithmetic on it with a double.

You probably want this instead:

inline FString operator+(const double num1, const FString &num2 )
{
	return FString(std::to_string(num1).c_str()) + num2;
}

oh, no, it did not work, still throws that error. updated question

Logger::log("Number " + doubleNumber + "~~~" );

"Number " isn’t an FString, it’s a const char[], so it’s still trying to do pointer arithmetic.

You will either need to do:
Logger::log(FString("Number ") + doubleNumber + "~~~" );

Or just use FString::Printf to do the formatting instead.

inline FString operator+(const double num1, const char num2[])
{
return FString(std::to_string(num1).c_str()) + num2;
}
inline FString operator+(const char num1[], const double num2)
{
return num1 + FString(std::to_string(num2).c_str());
}

error C2803: ‘operator +’ must have at least one formal parameter of class type

Once again, in your second example num1 is a const char[], not an FString, so there’s no + operator for it.

inline FString operator+(const char num1[], const double num2)
{
	return FString(num1) + FString(std::to_string(num2).c_str());
}

Since you’ve essentially asked me the same question three times, I’m guessing you’re a little confused about how strings work in C?

String literals aren’t magic, they’re just a variable of type const char[]. If you want fancy behaviour from them, you’ll need to convert them to some sort of string class, like FString.

Overloading a global operator for two intrinsic types (const char[] and double) is getting into really suspect behaviour which may come back to bite you later on. Are you sure you want to be doing this?

I am making a log function in which I write strings, numbers, boolean and everything I want to log (see updated question text) and coming from a flash background, using the + sign turns everything to string and concatenates it. Not in C++ apparently :expressionless: so I am trying to overload the + to accept pretty much any such combinations. Keeping it all as FString because why not. Am rather new at C++ and this pointer business is indeed confusing.

Just to make sure, but you do know that UE4 has logging support built in via UE_LOG?

Our more typical way to create a string like you’re doing in your question would be to use FString::Printf (which uses printf style formatting syntax) rather than overload operators (particularly the + operator since it produces a lot of copies).

FString::Printf(TEXT("Number %f ~~~"), doubleNumber);

I do know about the logs but they are drowned in a flood of other system messages. and showing messages in the editor is not efficient. The simplicity of calling a static function with a concatenation of whatever I want is what I crave. Is there any way to satiate the operator+ with the double value?

You could, but I’d limit the overloads to always having a const FString& on the left hand side.

Alternatively, how about something like this? This puts the overloads onto a custom type (FLogLine), and uses it as a scoped object returned from FLogger::Log to handle writing out a line of log text when the object falls out of scope.

this goes beyond my current knowledge. gonna need some time understanding that txt of c++ goodness :slight_smile: i will keep trying to figure out a way to overload operator+ for my cases :frowning: thank you