Compile errors in generated files ?

Hi, I have errors identified in generated files which I don’t understand and I’d love some insight.

I’m having issues with dotnet and packages at the same time and I’m not sure whether it’s affecting the build or not. I answered a post here with details.

(I replaced the name of the project with ****)

The errors :

Intermediate\Build\Win64\UnrealEditor\Inc\****\UHT\Pickup.generated.h(65): error C2951: template declarations are only permitted at global, namespace, or class scope
Intermediate\Build\Win64\UnrealEditor\Inc\****\UHT\Pickup.generated.h(65): error C2988: unrecognizable template declaration/definition
Intermediate\Build\Win64\UnrealEditor\Inc\****\UHT\Pickup.generated.h(65): error C2143: syntax error: missing ';' before '<'
Intermediate\Build\Win64\UnrealEditor\Inc\****\UHT\Pickup.generated.h(65): error C2071: 'StaticClass': illegal storage class
Intermediate\Build\Win64\UnrealEditor\Inc\****\UHT\Pickup.generated.h(65): error C2059: syntax error: '<'
Source\****\Actors\Pickups\Pickup.cpp(12): error C2027: use of undefined type 'APickup'
Source\****\Actors\Pickups\Pickup.h(12): note: see declaration of 'APickup'

This is followed by errors related to Pickup which seem to just be a consequence of the prior issue. (I can add them if needed)

I tried to delete the intermediate folder and rebuild everything, but it didn’t work. The error appeared out of the blue without me making any change to this class specifically.

Here’s everything just in case :

The line which seems to be the issue in the generated file :

template<> ****_API UClass* StaticClass<class APickup>();

.h

#pragma once

#include "CoreMinimal.h"

#include "Pickup.generated.h"

UCLASS(Blueprintable)
class ****_API APickup : public AActor
{
	GENERATED_BODY()

public:    
    APickup();    

protected:    
    virtual void BeginPlay() override;

    virtual void SetupOverlap();

    void ApplyEffects(AActor* Actor);

    UFUNCTION()
    void OnSphereBeginOverlap(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult);

    UPROPERTY(EditAnywhere)
    class USceneComponent* SceneRootComponent{ nullptr };
    UPROPERTY(EditAnywhere)
    class USphereComponent* SphereComponent{ nullptr };
   
    UPROPERTY(BlueprintReadOnly, EditAnywhere, Category = "Pickup")
    TArray<TSubclassOf<class UGameplayEffect>> PickupEffects;
};

.cpp

#include "****/Actors/Pickups/Pickup.h"

#include "Components/SphereComponent.h"
#include "Components/SceneComponent.h"

#include "****/AbilitySystem/Utility/NWkGASUtility.h"
#include "****/Characters/PlayerCharacter/PlayerCharacter.h"
#include "****/AbilitySystem/NWkAbilitySystemComponent.h"

APickup::APickup()
{
    PrimaryActorTick.bCanEverTick = false;

    SceneRootComponent = CreateDefaultSubobject<USceneComponent>(FName("Root"));
    RootComponent = SceneRootComponent;

    SphereComponent = CreateDefaultSubobject<USphereComponent>(TEXT("SphereComponent"));
    SphereComponent->SetupAttachment(RootComponent);
    SphereComponent->SetSphereRadius(100.f);
    SphereComponent->SetGenerateOverlapEvents(true);    
}

void APickup::BeginPlay()
{
    Super::BeginPlay();

    SetupOverlap();
}

void APickup::OnSphereBeginOverlap(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
    ApplyEffects(OtherActor);

    Destroy();
}

void APickup::SetupOverlap()
{
    SphereComponent->OnComponentBeginOverlap.AddDynamic(this, &APickup::OnSphereBeginOverlap);
}

void APickup::ApplyEffects(AActor* Actor)
{
    if (!Actor)
        return;

    APlayerCharacter* playerCharacter = Cast<APlayerCharacter>(Actor);
    if (!playerCharacter)
        return;

    UNWkAbilitySystemComponent* playerASC = playerCharacter->GetNWkAbilitySystemComponent();
    if (!playerASC)
        return;

    for (TSubclassOf<UGameplayEffect> Effect : PickupEffects)
    {
        NWkGASUtility::ApplyGameplayEffectToSelf(playerASC, Effect, this);
    }
}

遇到了同样的问题。在使用版本控制一个一个排查文件后,确定是某个cpp文件的问题,它的函数定义少了一个括号,奇怪的是编译器的报错完全不提及*.cpp,而是所有的*.generated.h都出现类似的报错。详细的情况是这样的:排查文件时发现xxx.cpp文件明明定义了NativePreConstruct()函数,但是xxx.h的函数声明总是警告此函数未定义,而且cpp文件只定义了NativePreConstruct()函数而没有其他内容,仔细检查后里面的if语句发现少了括号,像这样:

NativePreConstruct(){
    ...
    if{
        ...
} 

怀疑因为是最后一个定义的函数,缺少“}”后编译器在此cpp文件中找不着其它“}”字符,因此继续编译xxx.generated.h文件时,就认为文件里面的所有内容都属于未配对{}函数的定义范围内,而不幸的是xxx.generated.h文件没有“}”字符而先出现了template的定义(很明显它不能在函数内定义),因此对xxx.generated.h文件的报错覆盖了函数{}符未配对的错误。因此,你应该检查有什么奇怪的已经声明且定义,但是却显示未定义的函数声明,并且查它们的括号配对情况。