Unreal Engine 5 C++ Developer: Learn C++ & Make Video Games

I don’t know why I have no errors showing up in the compiler but am having one in unreal’s compilation.
As this header file is exactly as they have it in the lecture.

The errors are there but they’re not easy to see:

Have a look at those files and those lines of code. I’ve had this error before - its an easy fix but i forgot what caused it.

You are shadowing a member variable.

class AMovingPlatform : public AActor
{
    virtual void BeginPlay() override;
    FVector StartLocation; // member variable
};

void AMovingPlatform::BeginPlay()
{
    FVector StartLocation; // this defines a new variable local to this function
                           // it also "hides" the member variable.
                           // Uses of `StartLocation` here refer to this variable
                           // and not the member variable

} // local variable destroyed here
1 Like

Sorry I do not understand what ‘Shadowing’ means. Do I need to declate this member variable in the header file under a public access operator? In the C++ and header files of the lecture that seem to run fine for Sam they have the exact same lines as me- so I do not understand why he hasn’t been getting the same issue?

Ok! Breakthrough! Thank you. I just looked up ‘Shadowing variables’ and it looks like this is another term for ‘black boxing’. I realized that on line 40 in my CPP file I was declaring the variable again with the ‘FVector’ instantiation. So once I had removed that then I was trying to ‘shadow’ the variable anymore because I was now only using the private member variable of the class associated with that member function.

1 Like

Sorry, after typing out that demonstration code I completely forgot to finish what I was going to say :sweat_smile:. I was going to say that Unreal enables shadowing warnings and treats them as an error and secondly, that it is very likely that you have FVector in BeginPlay where there shouldn’t be.

This actually showcases why those warnings are enabled and treated as an error as it caught the bug you wrote.