C++ Widget PossiblyNullContent error

I’m getting the following error when trying to add a Widget to the Viewport:

error C2664: 'SWeakWidget::FArguments::WidgetArgsType &SWeakWidget::FArguments::PossiblyNullContent(TSharedPtr<SWidget,ESPMode::NotThreadSafe>)': cannot convert argument 1 from 'TSharedPtr<SMainMenuWidget,ESPMode::NotThreadSafe>' to 'TSharedPtr<SWidget,ESPMode::NotThreadSafe>'

MenuHUD.cpp:

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/HUD.h"
#include "MenuHUD.generated.h"

/**
 * 
 */
UCLASS()
class MENU_API AMenuHUD : public AHUD
{
	GENERATED_BODY()

protected:

	TSharedPtr<class SMainMenuWidget> MenuWidget;

	/** Container to hold the Menu Widget. We will add and remove from this container */
	TSharedPtr<class SWidget> MenuWidgetContainer;

	virtual void BeginPlay() override;
	
};

MenuHUD.cpp:

#include "MenuHUD.h"
#include "Engine/Engine.h"
#include "Widgets/SWeakWidget.h"
#include "SMainMenuWidget.h"

void AMenuHUD::BeginPlay()
{
	Super::BeginPlay();

	if (GEngine && GEngine->GameViewport)
	{
		// We create our Menu Widget and pass our HUD
		MenuWidget = SNew(SMainMenuWidget).OwningHUD(this);

		// SAssignNew it's the same as SNew but it lets us store it in a variable
		GEngine->GameViewport->AddViewportWidgetContent(SAssignNew(MenuWidgetContainer, SWeakWidget).PossiblyNullContent(MenuWidget));
	}
}

I also tried using .ToSharedRef() because I see everyone doing that, but I get the following error:

cannot convert argument 1 from 'TSharedRef<ObjectType,ESPMode::NotThreadSafe>' to 'TSharedPtr<SWidget,ESPMode::NotThreadSafe>'

What am I doing wrong? I’ve have seen examples but still can’t figure it out.

Thanks!

Hi there! What class SMainMenuWidget is inherited from?

Hello1. It’s an SCompoundWidget

#include "SlateBasics.h"
#include "SlateExtras.h"

class SMainMenuWidget : SCompoundWidget
{
public:

	SLATE_BEGIN_ARGS(SMainMenuWidget) {}

	SLATE_ARGUMENT(TWeakObjectPtr<class AMenuHUD>, OwningHUD);

	SLATE_END_ARGS()

	void Construct(const FArguments& InArgs);

	TWeakObjectPtr<class AMenuHUD> OwningHUD;

	virtual bool SupportsKeyboardFocus() const override { return true; };
};

It seems that you try to assign SWeakWidget value to SMainMenuWidget variable, this classes are different…

Found out the error.

I forgot the public keyword before SCompoundWidget when doing the inheritance in my SMainMenuWidget.