Mesh is private member

Maybe somebody can help me?
Mistake: ‘Mesh’ is a private member of ‘ACharacter’

“Learning c++ by creating games with ue4” book

//MeleeWeapon.h:

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "Monster.h"
#include "Runtime/Engine/Classes/Components/StaticMeshComponent.h"
#include "Runtime/Engine/Classes/Components/BoxComponent.h"
#include "Runtime/Engine/Classes/Components/PrimitiveComponent.h"
#include "MeleeWeapon.generated.h"

class AMonster;
UCLASS()
class NONAME_API AMeleeWeapon : public AActor
{
GENERATED_BODY()
public:	
	// Sets default values for this actor's properties

    AMeleeWeapon();
    AMeleeWeapon(const class FObjectInitializer& PCIP);

protected:
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;

public:	
	// Called every frame
	virtual void Tick(float DeltaTime) override;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category= "MeleeWeapon")
    float AttackDamage;
    TArray<AActor*> ThingsHit;
    bool Swinging;
    AMonster *WeaponHolder;
	
    UPROPERTY(VisibleDefaultsOnly, BlueprintReadWrite, Category= "MeleeWeapon")
    UBoxComponent *ProxBox;
    
    UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category= "MeleeWeapon")
    UStaticMeshComponent *Mesh;
    
    UFUNCTION(BlueprintNativeEvent, Category = "Collision")
    void Prox( class UPrimitiveComponent* HitComp, class AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult &  SweepResult );
    
    void Swing();
    void Rest();
};


//Monster.cpp:



#include "Monster.h"
#include "Avatar.h"
#include "Runtime/CoreUObject/Public/UObject/UObjectGlobals.h"
#include "Runtime/Engine/Classes/Kismet/GameplayStatics.h"
#include "Runtime/Engine/Classes/Engine/SkeletalMeshSocket.h"
#include "Runtime/Engine/Classes/Components/StaticMeshComponent.h"
#include "MeleeWeapon.h"

// Sets default values

AMonster::AMonster()
{
    // Set this character to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
    PrimaryActorTick.bCanEverTick = true;
    
}


void AMonster:: PostInitializeComponents()
{
    Super:: PostInitializeComponents();
    
    if(BPMeleeWeapon)
    {
        MeleeWeapon=GetWorld()->SpawnActor<AMeleeWeapon>(BPMeleeWeapon, FVector(),FRotator());
        
        if(MeleeWeapon)
        {
            const USkeletalMeshSocket *socket=Mesh->GetSocketByName("ik_hand_rSocket"); //MISTAKE
            socket->AttachActor(MeleeWeapon, Mesh ); //MISTAKE
        }
    }
}

by default everything in your class is private.

You need to set its Access Specifier.

Those private, protected, or public you see in header files.

MeleeWeapon.h:

 class Monster;
 Class AMeleeWeapon
 {
 ...
public:

    UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category= "MeleeWeapon")
    UStaticMeshComponent *Mesh;
 ...
 };

adding the public will let you access the Mesh from anywhere.


Quick breakdown of the 3:

  • public: Can be accessed from anywhere you can get a ref to this class
  • private: Even with a ref to this class these won’t show.
  • protected: Private but accessible from Child Classes.

Yes, thanks, I’m sorry, that is my fault , insufficient information about the problem,
I updated the information

Maybe you can try to help me again?

I see you’re declaring the UStaticMeshComponent in the .h, But are you actually creating the component anywhere?

Because you seem to be trying to access an empty pointer.

Sorry, my English isn’t good, but maybe you ask about this:

MeleeWeapon.cpp:

Mesh = PCIP.CreateDefaultSubobject(this, TEXT(“Mesh”)); RootComponent = Mesh;

That all from “Learning c++ by creating games with ue4” book, I’m just trying to repeat

The “Mesh” variable you are trying to access is not the one from the weapon but of the character so you should use GetMesh(); if you want to access the one from the weapon you should use “MeleeWeapon->Mesh” ( but i think thats nit the case )

Thank you!