Error C2355: using "this->value" in a struct function

I have a struct with an enum value and a function that converts said enum value to a string:

USTRUCT(BlueprintType)
struct FClassicNeed
{
	// UENUM
	EClassicNeedName Name;

	FString FClassicNeed::ReturnNameAsString(EClassicNeedName InName = this->Name)
	{
		// Long and probably unnecessary code
	}
}

However, using this->Name (or even this.Name) doesn’t work. I get an error C2355: 'this': can only be referenced inside non-static member functions or non-static data member initializers. I’ve tried it as well with FString ReturnNameAsString(...), but with the same error.

I don’t quite understand this, and Google/Stack Exchange didn’t have a working answer. What is wrong with that argument? The function is non-static, isn’t it?

Hi,

Default arguments are not allowed to refer to this or member variables in C++:

http://en.cppreference.com/w/cpp/language/default_arguments

The this pointer is not allowed in default arguments

Non-static class members are not allowed in default arguments

Also, FYI, your inline function should not be qualified with FClassicNeed:: - this will cause compile errors in any non-VC compiler.

Steve

Aww shucks. Well thanks!

You could use an overload instead:

FString ReturnNameAsString()
{
    return ReturnNameAsString(this->Name);
}

FString ReturnNameAsString(EClassicNeedName InName)
{
    // Long and probably unnecessary code
}

Steve