How to create userwidget in C++ with UE5? The CreateWidget() will cause compiling failure

Hi,
I tried to create a custom userwidget in C++, the code I used as below.
First I defined a custom userwidget class inherited from UserWidget Class, which is named USessionMenu.

Then in the BeginPlay event of PlayerController, I tried to create the widget

But it caused the compiling errors as below

I learned this in a UE4 totorial, but the code I write follow it can not work. What is wrong with my code? Please help.
Thanks.

The detailed error messages are:
1>MP_PlayerController.cpp.obj : error LNK2019: unresolved external symbol “__declspec(dllimport) private: static class UClass * __cdecl UUserWidget::GetPrivateStaticClass(void)” (_imp?GetPrivateStaticClass@UUserWidget@@CAPEAVUClass@@XZ) referenced in function “class UUserWidget * __cdecl CreateWidget<class UUserWidget,class UWorld *>(class UWorld *,class TSubclassOf,class FName)” (??$CreateWidget@VUUserWidget@@PEAVUWorld@@@@YAPEAVUUserWidget@@PEAVUWorld@@V?$TSubclassOf@VUUserWidget@@@@VFName@@@Z)
02:18:53:149 1> Hint on symbols that are defined and could potentially match:
02:18:53:149 1> “__declspec(dllimport) private: static class UClass * __cdecl APawn::GetPrivateStaticClass(void)” (_imp?GetPrivateStaticClass@APawn@@CAPEAVUClass@@XZ)
02:18:53:149 1> “__declspec(dllimport) private: static class UClass * __cdecl UCameraComponent::GetPrivateStaticClass(void)” (_imp?GetPrivateStaticClass@UCameraComponent@@CAPEAVUClass@@XZ)
02:18:53:149 1> “__declspec(dllimport) private: static class UClass * __cdecl UEnhancedInputComponent::GetPrivateStaticClass(void)” (_imp?GetPrivateStaticClass@UEnhancedInputComponent@@CAPEAVUClass@@XZ)
02:18:53:149 1> “__declspec(dllimport) private: static class UClass * __cdecl UEnhancedInputLocalPlayerSubsystem::GetPrivateStaticClass(void)” (_imp?GetPrivateStaticClass@UEnhancedInputLocalPlayerSubsystem@@CAPEAVUClass@@XZ)
02:18:53:149 1> “__declspec(dllimport) private: static class UClass * __cdecl UObject::GetPrivateStaticClass(void)” (_imp?GetPrivateStaticClass@UObject@@CAPEAVUClass@@XZ)
02:18:53:149 1> “__declspec(dllimport) private: static class UClass * __cdecl USessionMenu::GetPrivateStaticClass(void)” (_imp?GetPrivateStaticClass@USessionMenu@@CAPEAVUClass@@XZ)
02:18:53:149 1> “__declspec(dllimport) private: static class UClass * __cdecl USpringArmComponent::GetPrivateStaticClass(void)” (_imp?GetPrivateStaticClass@USpringArmComponent@@CAPEAVUClass@@XZ)
02:18:53:149 1>MP_PlayerController.cpp.obj : error LNK2019: unresolved external symbol “__declspec(dllimport) public: static class UUserWidget * __cdecl UUserWidget::CreateWidgetInstance(class UWorld &,class TSubclassOf,class FName)” (_imp?CreateWidgetInstance@UUserWidget@@SAPEAV1@AEAVUWorld@@V?$TSubclassOf@VUUserWidget@@@@VFName@@@Z) referenced in function “class UUserWidget * __cdecl CreateWidget<class UUserWidget,class UWorld *>(class UWorld *,class TSubclassOf,class FName)” (??$CreateWidget@VUUserWidget@@PEAVUWorld@@@@YAPEAVUUserWidget@@PEAVUWorld@@V?$TSubclassOf@VUUserWidget@@@@VFName@@@Z)
02:18:53:149 1>D:\Unreal Projects\MPShooterEOS\Binaries\Win64\UnrealEditor-MPShooterEOS.dll : fatal error LNK1120: 2 unresolved externals
02:18:53:149 1>[6/6] WriteMetadata MPShooterEOSEditor.target cancelled
02:18:53:266 1>C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Microsoft\VC\v170\Microsoft.MakeFile.Targets(44,5): error MSB3073: The command “E:\UE521\Engine\Build\BatchFiles\Build.bat -Target=“MPShooterEOSEditor Win64 Development -Project="D:\Unreal Projects\MPShooterEOS\MPShooterEOS.uproject"” -Target=“ShaderCompileWorker Win64 Development -Quiet” -WaitMutex -FromMsBuild” exited with code 6.
02:18:53:266 1>Done building project “MPShooterEOS.vcxproj” – FAILED.

I dont think you are supposed to actually create them, as far as I know at least. I create a BP that extends the C++ custom UserWidget. Create the UI in the BP and do all the code like handling clicks and animations, etc in the C++ code.

You can do this in a UserWidget class

UPROPERTY(BlueprintReadWrite, meta = (BindWidget))
UButton* QuitButton = nullptr; // this will require that the BP subclass must have a button called QuitButton in the designer hierarchy 

UPROPERTY(Transient, BlueprintReadWrite, meta = (BindWidgetAnim))
class UWidgetAnimation* Open = nullptr; // this will require that the BP subclass must have an animation called Open

Then in C++ you can do what you need with the animation and register click handlers, etc. That’s how I have come to understand how they are used at least.