Cannot figure why UE4 has error cannot access private member declared in class


Today in my evaluation of whether I can use UE4 as a platform to implement some ideas I have about using it to create a virtual world, I have found a road block that I quite do not understand for something I consider trivial and easy.

I am not yet highly familiar with how the UE4 C++ code implementation works, so I am trying to perform a simple task of calling a public function from one C++ Blueprint library I set up though the UE4 editor by another. I plan not to have these functions available as blueprint nodes in the editor. According to some documentation and a couple of youtube tutorials, I should be able to treat these as any ordinary c++ code. But when I compile the code I get the error

F:\Projects\Games\Voxel_CPP\Source\Voxel_CPP\Private\player\User_BPFL.cpp(10) : error C2248: 'UCameraComponent::UCameraComponent': cannot access private member declared in class 'UCameraComponent'

The Code this is referring to is the public function get_camera_direction_vectors in camera_BPFL.h

class VOXEL_CPP_API Ucamera_BPFL : public UBlueprintFunctionLibrary
	Fmovement_struct_type get_camera_direction_vectors(UCameraComponent camera);



Fmovement_struct_type Ucamera_BPFL::get_camera_direction_vectors(UCameraComponent camera) {
	Fmovement_struct_type camera_direction;
	FMinimalViewInfo camera_view;
	UKismetMathLibrary ml;


	camera_direction.forward_movement_vector = ml.GetForwardVector(camera_view.Rotation);
	camera_direction.side_movement_vector    = ml.GetRightVector(camera_view.Rotation);
	camera_direction.up_movement_vector      = ml.GetUpVector(camera_view.Rotation);

	return camera_direction;

and camera.GetCameraView is a public function within UCameraComponent as far as I can see.
This is called within the public function initialise_camera_direction_vectors of file User_BPFL.cpp

 Fmovement_struct_type Uuser_BPFL::initialise_camera_direction_vectors(UCameraComponent camera) {
        	Ucamera_BPFL camera_BPFL;
        	Fmovement_struct_type initial_movement_direction = camera_BPFL.get_camera_direction_vectors(camera);
        	return initial_movement_direction;

My C++ experience tells me this should compile, (as it does if the code of get_camera_direction_vectors is in User_BPFL.cpp without a function call) but when I last touched UE C++ some 18 months ago, I found it to be particularly fussy and very sensitive to trying to introduce or modify code it did not generate. So I exited the project as I thought something may have messed up with the editor or visual studio 15 and restart the project. I then got the following.

    the following modules are missing or built with a different engine version

would you like to rebuild them now?

So naturally I said yes and got

   Voxel_CPP could not be compiled. Try rebuilding from source manually.

This is the second time this has happened today. Not sure why.

Am I doing something fundamentally wrong or is it that UE4 cannot handle simple C++ fuction calls outside its strict macro regime?

The reason I am doing this is because I cannot find any C++ function that is equivalent to the vector functions GetforwardVector, GetRightVector and GetUpVector that exist as Blueprint nodes as shown blow.

Any help would be appreciated.

The parameter to your function is an object, but it should be a pointer. Change it so it takes a pointer to the camera component and change the inside of your function to use the pointer.

also the error occurs on line 10 of User_BPFL.cpp not between 1 to 7 :slight_smile:

I see what you did wrong now. I misread the first time. My answer is updated and it should be right this time.

And when it says “Voxel_CPP could not be compiled. Try rebuilding from source manually” it’s saying to build the project from Visual Studio. It used to happen a lot to me.

I actually see there are a bunch of spots you need to change so they work with pointers. The camera will always be a dynamically allocated object so it will be represented as a pointer. Right now you are telling the compiler to create new camera objects on the stack. When it tries to do that, it calls the constructor but the constructor is private so it fails.

Well, after digging around a little more, (documentation is hard to come by and not easy to find) I found what was required to be done. Simple once one has the knowledge.

Apparently any function in a function library to be called by other functions has to be declared as a static function, and to see that function appear as a blueprint node, the function must be preceded by the macro statement as follows

UFUNCTION(BlueprintCallable, Category = "category")
static return_type function_name(parameters);

And the problem with my project not being able to be loaded into the editor. Well it looks that is because the project was exited when there were compile errors not resolved. Once I fixed up the compilation errors in VS2015, I could then restart the UE4 project. A bug perhaps and to note. Never leave UE4 editor if compilation errors are present.

Thanks for the replies.

Fmovement_struct_type get_camera_direction_vectors(UCameraComponent camera)

^^^ That is not correct. And neither is everywhere else you use components without pointers.

It should be “Fmovement_struct_type get_camera_direction_vectors(UCameraComponent* camera)” and all the code in the function needs to deal with pointers too (i.e. changing X.Y into X->Y).

Do you understand pointers? You can’t pass a component as a parameter because they can only be created dynamically. You have to pass in its pointer. I’m not sure why you won’t listen or respond to me directly, but GL, I’m done…