Compiler error when creating static const FString member variable

Inside my custom GameInstance class, I want to create a variable in the .h

public:
	static const FString PartyGameModeString = FString::Printf(TEXT("Party"));

But the compiler gives me the following error:

error C2864: 'UPartiesGameInstance::PartyGameModeString': a static data member with an in-class initializer must have non-volatile const integral type
note: type is 'const FString'

Any ideas what I should do instead?

Also as you might be able to guess, I am not 100% sure when to use const vs. constexpr, but I believe static const and static constexpr is the same in most cases. It would be great if someone would clear that up.

Thank you so much!

I now put the string in the header file, but outside the class definition:

const FString PartyGameModeString(TEXT("Party"));

UCLASS()
class ROBOTDEFENSECPP_API UPartiesGameInstance : public USessionInterfaceGameInstance
{
    ...
};

Please let me know if you think if that’s a good solution and a good place to put that string. I am by no means an expert programmer, so I could definitely need some encouragement.

Wow, it’s been a long time since I posted this question. I totally forgot about it.

The correct solution to this problem was to declare and define the variable like so:

PartiesGameInstance.h:

class YOURPROJECT_API UPartiesGameInstance : public USessionInterfaceGameInstance
{
    ...
    static const FString PartyGameModeString;
    ...
};



PartiesGameInstance.cpp:

const FString UPartiesGameInstance::PartyGameModeString = FString::Printf(TEXT("Party"));
2 Likes

Just to be clear, UE4.16.1 doesn’t support const as a statement or Const as a UFUNCTION specifier. So you hardly can expose your FString in BP (well, through a mutator/getter). And, of course, if you want to make the variable global, don’t forget to add static.