Mark
Mark

Reputation: 47

The createdefaultsubobject function does not work for UWidgetComponent[UE4.26.2]

When creating an object of type UWidgetComponent in the constructor, it throws a compilation error. How to solve this problem?
changed different variations of the PROPERTY macro didn't help also tried to use the New Object function to create an object also didn't help

//.h

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

UCLASS()
class TESTPROJECT_API ATestWidgetActor : public AActor
{
    GENERATED_BODY()
    
public: 
    // Sets default values for this actor's properties
    ATestWidgetActor();

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

    UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Test")
        class UWidgetComponent* TestWidgetComponent;
    
public: 
    // Called every frame
    virtual void Tick(float DeltaTime) override;

};

//.cpp

#include "TestWidgetActor.h"
#include "Components/Widget.h"
#include "Components/WidgetComponent.h"

// Sets default values
ATestWidgetActor::ATestWidgetActor()
{
    // Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
    PrimaryActorTick.bCanEverTick = true;
    this->TestWidgetComponent = CreateDefaultSubobject<UWidgetComponent>("Widget component");
    this->TestWidgetComponent->SetupAttachment(GetRootComponent());
}

//Error

Error LNK2019 reference to an unresolved external character "__declspec(dllimport) private: static class Class * __cdecl UWidgetComponent:: GetPrivateStaticClass(void)" (__imp_?GetPrivateStaticClass@UWidgetComponent@@CAPEAVUClass@@XZ) in the function "public: __cdecl ATestWidgetActor::ATestWidgetActor(void)" (?? 0ATestWidgetActor@@QEAA@XZ). Test Project C:\Users\mark\Documents\Unreal Project\Test Project\Intermediate\Project Files\Test Widget Actor.cpp.obj 1

Upvotes: 0

Views: 4539

Answers (1)

Thomas MacCort
Thomas MacCort

Reputation: 36

The function is in different module and you didn't add dependency of that module in build script (*.build.cs file)

pic.jpg

Upvotes: 2

Related Questions