I have experienced a strange issue writing the constructor of UUserWidget-inherited class. The problem appears only (checked in several other) in this class. Code: .h .cpp UserWidget.h CompilingResult
Asked
Active
Viewed 1,632 times
0
-
1Sometimes your classes should be `DefaultConstructible`: http://en.cppreference.com/w/cpp/language/default_constructor – matheussilvapb Jan 20 '18 at 17:32
-
Doesn't seem to work - the same result – Sheim Jan 20 '18 at 18:27
-
1Do you get a compiler error? It's not quite clear what is your problem here. Maybe you can tell us what is the compiler yelling about. – matheussilvapb Jan 20 '18 at 18:34
-
Oh, I'm sorry - just forgot to attach this picture – Sheim Jan 20 '18 at 18:41
2 Answers
4
Try to add this to your base class (UUserWidget):
UUserWidget();
That's because there is no default constructor for this class. The default constructor is one which takes no parameters.
![](../../users/profiles/9244918.webp)
DankMemesBot
- 88
- 6
-
There already is a constructor `UUserWidget(const FObjectInitializer &ObjectInitializer);` why might it not be a default constructor? – Sheim Jan 20 '18 at 18:44
-
2Because the default constructor does not take any parameters, so UUserWidget(const FObjectInitializer &ObjectInitializer); can't be a default constructor. – DankMemesBot Jan 20 '18 at 19:00
-
Similar result as with the previous constructor (I still don't know how might it help me - overloading shouldn't work in this case?) – Sheim Jan 20 '18 at 19:08
-
1Are you sure?, I'm almost sure that this is what causing your problem... Your UUserWidget class should be like: public: UUserWidget(); and then UUserWidget(const FObjectInitializer &ObjectInitializer); – DankMemesBot Jan 20 '18 at 19:24
-
I think that i did it wrong last time. Now there is still a compilation problem, but different: [Result](https://prnt.sc/i37o8j) [UserWidget.h](https://prnt.sc/i37o8j) – Sheim Jan 20 '18 at 19:50
-
2You've got a linkage error, most of them caused from including the classes incorrectly, this isn't about the constructors anymore. – DankMemesBot Jan 20 '18 at 19:57
-
Ok I have solved the issue. [.cpp](http://prntscr.com/i38hbc) Nothing else was needed. Thanks for your suggestions! – Sheim Jan 20 '18 at 20:45
-
1@Sheim This is more of a specific Unreal Engine issue. You don't want to be adding code to the engine (as stated before). Essentially the difference is, If you look at Actor for example, it has a default () constructor. Whereas the UUserWidget does not. So you will need to just ensure you use the FObjectInitializer constructor instead. Should do the same job. Perhaps this is an oversight of Unreal Engine during their upgrade to allowing empty constructors. ```MyWidget::Mywidget(const FObjectInitializer& ObjectInitializer) : UUserWidget(ObjectInitializer){``` – Jimmyt1988 Dec 20 '19 at 16:20