【问题标题】:Compile errors for C++ Battery Collector tutorialC++ Battery Collector 教程的编译错误
【发布时间】:2019-07-02 15:32:11
【问题描述】:

抱歉,收到了一个非常菜鸟的问题,我一直在处理来自 Unreal 的 C++ Battery Collector tutorial,但我在教程中的代码中遇到了一些编译问题。

我一直在 Visual Studio 中跟进,在头文件中创建访问器方法时,我使用 Intelli 感知来生成对应的类。

Pickup.h

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "Pickup.generated.h"

UCLASS()
class BATTERYCOLLECTOR_API APickup : public AActor
{
    GENERATED_BODY()

public: 
    // Sets default values for this actor's properties
    APickup();

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

    // Return the mesh for the pickup
    FORCEINLINE class UStaticMeshComponent* GetMesh() const { return PickupMesh; }

    // Return whether or not the pickup is active
    UFUNCTION(BlueprintPure, Category = "Pickup");  // <--- error in this line
    bool IsActive();

    // Allows other classes to safely change whether or not the pickup is active 
    UFUNCTION(BlueprintCallable, Category = "Pickup");
    void setActive(bool NewPickupState);

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

    //True when the pickup can be used, and false when pickup is deactivated
    bool bIsActive;

private:
    //Static mesh to represent the pickup in the level
    UPROPERTY(visibleAnywhere, BlueprintReadOnly, Category = "Pickup", meta = (AllowPrivateAccess = "true"))
    class UStaticMeshComponent* PickupMesh;

};

Pickup.cpp

#include "Pickup.h"

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

    // All pickups start active
    bIsActive = true;

    // Create the static mesh component
    PickupMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("PickupMesh"));

}

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

}

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

}

// Visual studio created this definition 
//APickup::UFUNCTION(BlueprintPure, Category)
//{
    //What is should go in here?
//}

// Returns active state
bool APickup::IsActive()
{
    return bIsActive;
}

// Changes active state
void APickup::setActive(bool NewPickupState)
{
    bIsActive = NewPickupState;
}

在我在 .cpp 文件中添加注释 Visual studio created this definition 的那一行,这是由 Visual Studio 代码添加的定义,但我不确定它应该用于什么,没有它就无法编译。 (教程中没有这个定义,但是 VS 要我添加它。)

据我所知,我已经为这两个 UFUNCTIONS 定义了方法,但是如果我没有生成的定义,我会在虚幻引擎中得到编译错误。

D:/Unreal Projects/BatteryCollector/Source/BatteryCollector/Pickup.h(25) : Error: Function return type: Missing variable type

谁能看到我做错了什么或遗漏了什么?

【问题讨论】:

  • @un 码在您的问题中输入错误消息,而不是提供带有错误的图像。
  • 删除图片并添加文本版本。
  • Pickup.h - UFUNCTION(BlueprintPure, Category = "Pickup");
  • 没用过Unreal-engine,但是我注意到SetActive和IsActive之间是有区别的,IsActive的bool前面有个分号?是预期的吗?
  • @foreknownas_463035818 OP 也将分号添加到其他函数中。请 OP,删除两个分号。

标签: c++ unreal-engine4


【解决方案1】:

来自Unreal documentation

UFUNCTION([specifier, specifier, ...], [meta(key=value, key=value, ...)])
ReturnType FunctionName([Parameter, Parameter, ...])

该声明中没有;,很可能这就是问题所在。

请注意,在编辑问题的过程中,您还在另一个声明中添加了 ;,但您应该删除两者。

PS:我将此作为答案发布,希望将半垃圾邮件的 cmets 变成有用的东西。我不知道虚幻,也许这个答案是错误的,那就告诉我吧。

【讨论】:

  • 是的,你是对的,不需要的分号。 VS 想要添加一些额外的定义,因为该行已终止。
  • 这个答案是正确的。 UFUNCTION 宏充当装饰器,将额外的元数据添加到函数的声明中,然后由虚幻的构建工具和编辑器使用。即使它通常写在单独的行上,它也是声明的一部分,不应该用分号分隔。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-05-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-19
  • 1970-01-01
  • 2023-04-08
相关资源
最近更新 更多