Help! The letter "L" is haunting my code!

Hello UE4 forums!

Just to clarify, the title isn’t a joke.

I have this one line of code which is a print test to the log:


UE_LOG(LogTemp, Log, TEXT(std::to_string(charge)));

It looks completely fine, and have gone over it loads, however i get a wierd compiler output:


Lstd is not a class or namespace name

so i decided to rearrange my code to the following:


std::string s = std::to_string(charge);
	UE_LOG(LogTemp, Log, TEXT(s));


'Ls': undeclared identifier

So it seems as if i always have an “L” hidden in that line of code. Why is this happening?

TEXT is a macro that takes two tokens, L and whatever value is passed to the macro and pastes them together. Since you passed it ‘s’, the compiler sees “Ls”.

To fix your issue, you’ll want to format your string with TEXT:



std::string s = std::to_string(charge);
UE_LOG(LogTemp, Log, TEXT("%s"), s.c_str());


Hi!

Thanks for your response! It’s compiling correctly, but i get some wierd characters instead of numbers. What can i do?

Try using TEXT("%d") and pass in charge instead of s.c_str() or use the UE4 FString type and do *FString::FromInt(charge) or *FString::SanitizeFloat(charge) instead of s.c_str();

I am assuming charge is not a char* or const char* variable. You are expecting the std::string(charge) constructor to just know it is a number you are passing in and format it for you. This is wrong.