x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

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<class UWidgetComponent> WidgetComp = nullptr;

Trying to compile will result in an error, that "TWeakObjectPtr can only be constructed with UObject types".

Solution

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:

o 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         ]
Product Version: UE 4.12
Tags:
more ▼

asked Jul 26 '16 at 01:10 PM in Bug Reports

avatar image

AllJonasNeeds
249 26 36 71

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

2 answers: sort voted first

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

more ▼

answered Jul 26 '16 at 02:02 PM

avatar image

Shadowriver
37.3k 936 172 1116

avatar image AllJonasNeeds Jul 26 '16 at 02:08 PM

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?

avatar image Shadowriver Jul 26 '16 at 02:19 PM

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

avatar image AllJonasNeeds Jul 26 '16 at 02:21 PM

Cool, thank you.

avatar image Matthew J Aug 01 '16 at 03:44 PM

Hello everyone,

I've converted Shadowriver's 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!

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

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!

more ▼

answered Aug 23 '17 at 02:16 AM

avatar image

huzhongyidihao
1 2 1

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question