My funtion is inaccessible (while I think it's not)

Hello,

I was making a pointer to one of my class headers, and it says that the function is inaccessible, while I certainly think it is not. I am new to UE4 and C++ so this is probably a big derp, but I am stuck with this for a few hours now.

FPSCharacter class where is try to use the funtion InitVelocity:

// Spawn the fireball at the FireballLocation
			AFPSProjectile* const Projectile = World->SpawnActor<AFPSProjectile>(ProjectileClass, FireballLocation, FireballRotation, SpawnParams);
			if (Projectile)
			{
				// Find the launch directions
				FVector const LaunchDir = FireballRotation.Vector();
				Projectile->InitVelocity(LaunchDir);
			}

And this is the projectile header:
// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "GameFramework/Actor.h"
#include "FPSProjectile.generated.h"

/**
 * 
 */
UCLASS()
class FPSPROJECT_API AFPSProjectile : public AActor
{
	GENERATED_BODY()
		UPROPERTY(VisibleDefaultsOnly, Category = Projectile)
		TSubobjectPtr<USphereComponent> CollisionComp;
	
	/** Projectile movement component */
	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Movement)
	TSubobjectPtr<class UProjectileMovementComponent> ProjectileMovement;

	void InitVelocity(const FVector& ShootDirection);
};

And this is in the class FPSProjectile with the funtion InitVelocity declaration IN IT (Hahah, get it? :P).

void AFPSProjectile::InitVelocity(const FVector &ShootDirection)
{
	if (ProjectileMovement)
	{
		// Sets the projectile's velocity to the desired direction
		ProjectileMovement->Velocity = ShootDirection * ProjectileMovement->InitialSpeed;
	}
}

I hope you guys can help me fix this! :smiley:

Greetz,

Hey -

Which version of the engine are you working in? Also, when you say that the function is inaccessable what is the exact error message you receive when it tries to call the function? If you could describe the behavior that you’re getting it will help determine what the cause of the problem is.

Cheers

Hello ,
Thanks for the reply!
I am using engine version 4.6 (Because of a mouse glitch when I try to edit values inside the editor) and the excact error is:

Error	12	error C2248: 'AFPSProjectile::InitVelocity' : cannot access private member declared in class 'AFPSProjectile'	C:\Users\Mats\Documents\Unreal Projects\FPSProject 4.6\Source\FPSProject\FPSCharacter.cpp	111	1	FPSProject

Hey -

If you add the access specifier “public:” below the line of “GENERATED_BODY()” your source file should be able to see the function properly.

Cheers

Hello ,
This did work, thanks allot! :smiley:
But can you maybe explain why this class needed that and other don’t?

Because I did declare functions in headers before and they didn’t need public:

Thanks Again!

There was a code change in 4.6 that changed the line from GENERATED_UCLASS_BODY() to GENERATED_BODY(). The assumed specifier with the update is private however the assumed specifier with the older version (UCLASS) was public, thus why it wasn’t explicitly needed. Stating whether something is public, private, or protected will generally make it easier to follow the flow of the programming overall.

Ah amazing! Thanks !

I have the same problem.I’m using a TArray(TArray is type of MyClass)
nothing is out of ordinary.All variables and Methods are public.but i still get the error C2248: ‘MyClass::MyClass’ : cannot access private member declared in class ‘MyClass’.
I’m using Unreal 4.9
can anyone please help me?!

Hey Alireza.fz93-

I’m not sure I understand what you mean by the TArray is type of MyClass. Can you post the class files(.h and .cpp) for the class that is throwing this error?

Cheers