Hi everyone. I encountered some compile errors. I am not sure it was VS intellisense issue or I missed something.
E.g: when I called “ULocalPlayer” in my controller cpp file, I got this error "C2027 use of undefined type ‘ULocalPlayer’ ". Although it could be fixed by including Engine/LocalPlayer.h head file, I suppose it’s unnecessary since nearly all tutorial codes I found online didn’t include it. Actually, I could run my project normally , if I run my code directly. These errors only occur when I compile the project. It seems that I need to include endless head files to avoid errors, which is really annoying.
I am wondering how to avoid this issue. Thank you for any suggestions.
It’s true that VS intellisense is extremely slow sometimes and doesn’t always notify when a class is undefined, but you can’t access a class if it’s not included. As “annoying” as it can be, sometimes you just have to add a lot of #includes, that’s how c++ works.
You don’t have to include a class again if it’s already included in one of your included headers.
The thing is, usually you would use forward declaration in header files class ULocalPlayer instead of #include Engine/LocalPlayer.h and then include it in its C++ file, which confuses intellisense cause it can see the class, but you don’t have the full definition. This is what causes the need to include classes a bunch of times, but it’s prefered for clarity and control.
Doing it this way, headers are loaded faster, without the need of bringing all its included headers into every class that includes them, and also makes other C++ files not dependant on other headers’ includes.
I understood that I need to include header files and I did use forward declearations in my .h file. My case was that I had to include tons of unnecessary header files.
Some specifics issues I encountered:
I even had to include "Components/SkeletalMeshComponent.h" in my character cpp file, otherwise I got the error “use of undefined type ‘USKeletalMeshComponent’”. I was pretty sure it had been included in the parent ACharacter class, but it just didn’t work.
When I implemented enhanced input in my controller file, I added all necessary header files such as "EnhancedInputSubsystems.h" and "EnhancedInputComponent.h", but I had to include "Engine/LocalPlayer.h" and "GameFramework/Pawn.h", or the code below would got compile error:
// Compile error: use of undefined type 'ULocalPlayer'
UEnhancedInputLocalPlayerSubsystem* System = ULocalPlayer::GetSubsystem<UEnhancedInputLocalPlayerSubsystem>(GetLocalPlayer());
// Compile error: use of undefined type 'APawn'
if (APawn* ControllerPawn = GetPawn<APawn>())
{
ControllerPawn->AddMovementInput(ForwardDirection, InputValue.Y);
ControllerPawn->AddMovementInput(RightDirection, InputValue.X);
}
As you can see, I didn’t think the header files such as USKeletalMeshComponent or ULocalPlayer were needed here since they were included in parent class already.
Actually my project runs perfectly as long as I don’t press compile button. But it is annoying that I can’t compile the code to make sure everything is correct before running the code.