Unresolved external symbol when trying to add widget to the viewport

I want to add widget to the viewport using c++, but I’m getting error:

Act_31.cpp.obj : error LNK2019: unresolved external symbol “__declspec(dllimport) private: static class UClass * __cdecl UUserWidget::GetPrivateStaticClass(void)” (_imp?GetPrivateStaticClass@UUserWidget@@CAPEAVUClass@@anonymous_user_9674a66c) referenced in function “private: void __cdecl AAct_31::GameCicle(void)” (?GameCicle@AAct_31@@AEAAXXZ)

What I am doing wrong?

.h

class UUserWidget;

UCLASS()
class HOME_API AAct_31 : public AActor
{
    GENERATED_BODY()

    UFUNCTION() void GameCicle();

public: 
    AAct_31();

    class UUserWidget* blackLinesWidget;
    class UClass* blackLinesWidgetClass;

    virtual void Tick(float DeltaTime) override;

protected:
    virtual void BeginPlay() override;
};

.cpp

void AAct_31::Tick(float DeltaTime)
{
    Super::Tick(DeltaTime);
    GameCicle();
}

void AAct_31::GameCicle()
{
    FStringClassReference blackLinesWidgeClasstRef(TEXT("/Game/Blueprints/UI/blackLines"));
    blackLinesWidgetClass = blackLinesWidgeClasstRef.TryLoadClass<UUserWidget>(); // Error

    if (blackLinesWidgetClass)
    {
        blackLinesWidget = CreateWidget<UUserWidget>(this->GetGameInstance(), blackLinesWidgetClass);
        if (blackLinesWidget)
            blackLinesWidget->AddToViewport();
    }   
}
1 Like

Hi,

Hope this helps.

You will have to go to your Build.cs file and add “UMG” to the PublicDependencyModulesNames.

Also add “Slate” and “SlateCore” to PrivateDependencyModuleNames.

It should look like this:

5 Likes

It helped, thank you very much! But where could I find it? There is no documentation about it

You’re welcome. Yeah it’s in the docs :

PS. You don’t need to forward declare class UUserWidget above UCLASS() because you’ve already done so in the public section of your header.