指向不完整 class 类型的指针是不允许的 Unreal Engine 4.26

Pointer to incomplete class type is not allowed Unreal Engine 4.26

我尝试在我的游戏中为玩家实现 pawn 导航。当我在 BP 网站上尝试时,它工作得很好,但我尝试用 C++ 代码转换它。我得到了一种 wird 错误。在此之前,我遇到了另一个错误,但我发现导航系统在我的 ue 版本中发生了一些变化,但我通过更改为 UNavigationSystemV1 解决了问题。它可能会干扰我的 class?

NavPath 指针给了我错误。

这是错误列表:

> Error (active)    E0393   pointer to incomplete class type is not allowed 37
> Error (active)    E0393   pointer to incomplete class type is not allowed 40
> Error C2027   use of undefined type 'UNavigationPath'37
> Error C2027   use of undefined type 'UNavigationPath' 40

以下是有问题的部分代码:

FVector ASTrackerBot::GetNextPathPoint()
{
ACharacter* PlayerPawn =  UGameplayStatics::GetPlayerCharacter(this, 0);

UNavigationPath* NavPath = UNavigationSystemV1::FindPathToActorSynchronously(this, 
GetActorLocation(), PlayerPawn);

if (NavPath->PathPoints.Num() > 1)
{

    return NavPath->PathPoints[1];
}


return GetActorLocation();

}

这是我的整个私有 cpp 文件:

#include "AI/STrackerBot.h"
#include "Components/StaticMeshComponent.h"
#include "GameFramework/Character.h"
#include "Kismet/GameplayStatics.h"
#include "Runtime\NavigationSystem\Public\NavigationSystem.h"
#include "Runtime/Engine/Classes/Components/InputComponent.h"


// Sets default values
ASTrackerBot::ASTrackerBot()
{
    // Set this pawn to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
    PrimaryActorTick.bCanEverTick = true;

    MeshComp = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("MeshComp"));
    MeshComp->SetCanEverAffectNavigation(false);
    RootComponent = MeshComp;
}

// Called when the game starts or when spawned
void ASTrackerBot::BeginPlay()
{
    Super::BeginPlay();
    
}

FVector ASTrackerBot::GetNextPathPoint()
{
    // parcurgere drum pana la locatia playerului
    ACharacter* PlayerPawn =  UGameplayStatics::GetPlayerCharacter(this, 0);
    
    UNavigationPath* NavPath = UNavigationSystemV1::FindPathToActorSynchronously(this, GetActorLocation(), PlayerPawn);

    if (NavPath->PathPoints.Num() > 1)
    {
        // Return next point in the path
        return NavPath->PathPoints[1];
    }

    // nu a reusti sa gaseasca drumul
    return GetActorLocation();
}

// Called every frame
void ASTrackerBot::Tick(float DeltaTime)
{
    Super::Tick(DeltaTime);

}

这是我的头文件:

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Pawn.h"
#include "STrackerBot.generated.h"

UCLASS()
class GAME_API ASTrackerBot : public APawn
{
    GENERATED_BODY()

public:
    // Sets default values for this pawn's properties
    ASTrackerBot();

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

    UPROPERTY(VisibleAnywhere, Category = "Components")
    UStaticMeshComponent* MeshComp;

    FVector GetNextPathPoint();

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

};

UNavigationPath 的定义需要在您 NavPath->PathPoints 的地方可用。没有定义,编译器如何知道 UNavigationPath 甚至有一个名为 PathPoints 的成员?您可以 拥有 一个指向已声明但未定义的 class 的指针(即,您定义的 class UNavigationPath; 没有正文)并传递它,但您不能访问其任何成员。找到定义 UNavigationPath 的头文件并确保它包含在您的源代码中。

您可能还需要将 NavigationSystem 模块添加到 Project.Build.cs 文件的 PublicDependencyModuleNames 数组中。