TWeakObjectPtr compile error: UWidgetComponent is not a UObject?

Reproduction Steps

  1. Create MyActorComponent Class

  2. Include “UMG” in the *.Build.cs file

  3. Add the following line to your MyActorComponent.h

    TWeakObjectPtr WidgetComp = nullptr;
    Trying to compile will result in an error, that “TWeakObjectPtr can only be constructed with UObject types”.


Setting the default value in the constructor instead of the header file does not result in a compiler error. Another possible solution is including “Components/WidgetComponent.h” in the header file, which I wouldn’t recommend if you don’t understand circular dependency.

My Thoughts:

So I guess it is a bad practice to set the default value in the header file. But the behaviour is definately inconsistent, as the following line would not result in a compiler error. That’s why I thought I would share my findings.

TWeakObjectPtr<USceneComponent> WidgetComp = nullptr;

Full Compiler Error Log for Google:

CompilerResultsLog: Info c:\program files (x86)\epic games\4.12\engine\source\runtime\core\public\uobject\WeakObjectPtrTemplates.h(49): error C2338: TWeakObjectPtr can only be constructed with UObject types
CompilerResultsLog: Info c:\program files (x86)\epic games\4.12\engine\source\runtime\core\public\uobject\WeakObjectPtrTemplates.h(44): note: while compiling class template member function 'TWeakObjectPtr<UWidgetComponent,FWeakObjectPtr>::TWeakObjectPtr(const T *)'
CompilerResultsLog: Info         with
CompilerResultsLog: Info         [
CompilerResultsLog: Info             T=UWidgetComponent
CompilerResultsLog: Info         ]
CompilerResultsLog:Error: Error c:\users\user\documents\unreal projects\weakobjectptr_bug\source\weakobjectptr_bug\MyActorComponent.h(25) : note: see reference to function template instantiation 'TWeakObjectPtr<UWidgetComponent,FWeakObjectPtr>::TWeakObjectPtr(const T *)' being compiled
CompilerResultsLog: Info         with
CompilerResultsLog: Info         [
CompilerResultsLog: Info             T=UWidgetComponent
CompilerResultsLog: Info         ]

This error can apper also then you put undefined type or incomplete type where compiler does not know the size of stricture (or rether your forward reference declertion does not inherent UObject)

So it is something wrong with forward refrence

Are you saying, that I could make a forward declaration, that inherits from UObject to avoid the error?

// forward declaration:
class UWidgetComponent : public UObject;

Edit: By the way do you think setting variable defaults in the header is bad practice?

To tell the truth i noobish on that matter so i’m not sure, but at worst it might not solve a issue for compiler.

By convention defaults should be in constructor, so putting defaults in header is unconventional, it may work but using it with reflection system might mess up things, note for each UObject there is class default object (CDO) made when start engine and things can mess up there

Cool, thank you.

Hello everyone,

I’ve converted post to an answer as this seems to be resolved. If I’m wrong, please feel free to leave a comment and I’ll look into the issue further.

Have a nice day!

Good friends, I also met this problem. I am following the program code, but it doesn’t compile.

I see the way you brothers, so I put the structure of the construction method in the implementation of the CPP file was compiled successfully!