cannot convert 'this' pointer from 'const BLAH' to 'BLAHInterface &'

Hi Community,

I’m having trouble returning a private variable from an Interface. I have a private enum variable ‘EBZGame_CameraViewType’ in an interface, which my Character inherits from. I also have a function in the interface that is supposed to return the current state of that variable, from the inheriting classes instance of the interface (if possible).

My ‘getter’ function in the interface is causing me the following compiler error, which I can’t figure out. Even making the Variable ‘Public’ in the interface doesn’t make a difference, and returning it directly without the ‘Getter’ function just results in ‘Cannot Access Private Member in Interface’. Here’s the current compiler error:



1>E:\Users\James\Documents\Unreal Projects\BZGame\Source\BZGame\Private\Characters\BZGame_Character.cpp(110): error C2662: 'EBZGame_CameraViewSetting IBZGame_GameObjectInterface::GetObjectCameraViewSetting(void)' : cannot convert 'this' pointer from 'const ABZGame_Character' to 'IBZGame_GameObjectInterface &'


Here are the Interface Code Files:

Interface .H:



UINTERFACE(MinimalAPI)
class UBZGame_GameObjectInterface : public UInterface
{
	GENERATED_UINTERFACE_BODY()
};

class BZGAME_API IBZGame_GameObjectInterface
{
	GENERATED_IINTERFACE_BODY()
	virtual EBZGame_CameraViewSetting GetObjectCameraViewSetting();
	virtual void SetObjectCameraViewSetting(EBZGame_CameraViewSetting NewViewSetting);

	/* Set View Type For The Object, can be overridden by inherited classes to do either A) F*ck All, or B) Move the camera to a different view style */
	virtual void UpdateCameraViewType();

private:
	EBZGame_CameraViewSetting ObjectCameraViewSetting;
};


Interface .CPP



void IBZGame_GameObjectInterface::UpdateCameraViewType()
{
	// DO NOUT
}

EBZGame_CameraViewSetting IBZGame_GameObjectInterface::GetObjectCameraViewSetting()
{
	return (EBZGame_CameraViewSetting) this->ObjectCameraViewSetting;
}

void IBZGame_GameObjectInterface::SetObjectCameraViewSetting(EBZGame_CameraViewSetting NewViewSetting)
{
	ObjectCameraViewSetting = NewViewSetting;
	UpdateCameraViewType();
}


And here’s the relevant code in the Character CPP Class (I have an override function declaration in the .h file).



bool ABZGame_Character::IsFirstPerson() const
{
	return Controller && Controller->IsLocalPlayerController() && (GetObjectCameraViewSetting() != EBZGame_CameraViewSetting::ECVS_FirstPerson);
}


Any ideas? I’m not a C++ Wizard so might require a good explanation, thanks :slight_smile:

Solved

As usual the solution was simple… the variable had to be ‘protected’ rather than ‘private’, and then I can get it directly without using a getter function! Awesome!

This also makes sense, I’m technically inheriting from the Interface so it’s child class won’t be able to access a private member… awesome.