Constructor call to pure virtual class is deleted











up vote
-3
down vote

favorite












I'm trying to make a UI, and I want objects to inherit properties. The way I do this is I have a templated class Widget which takes a variable amount of types that it inherits from (see code below).



However, when I try to make a class that inherits from Widget<Resizeable> I get an error: The default constructor of Widget<Resizeable> is deleted.



Could someone explain why this happens? Widget and Resizeable are both abstract, so why is the constructor being called?



Widget:



template<typename... B>
class Widget : public B...
{
public:
virtual void Move(Vector position) = 0;
virtual void Show(bool show) = 0;

template<typename T>
bool HasBehavior()
{
return std::is_base_of<T, std::remove_reference<decltype(*this)>::type>::value;
}
protected:
// protected members
};


Resizeable:



class Resizeble
{
public:
virtual void Resize(Vector size) = 0;

Vector Size() const noexcept
{
return size_;
}
protected:
Vector size_;
};


Rectangle:



class Rectangle : public Widget<Resizeble>
{
public:
Rectangle(Vector position, Vector size, Color color) : color_(color)
{

}
private:
Color color_;
};


SOLUTION:



Implement constructors for all base classes, and use virtual inheritance so that the most derived class (in this case, Rectangle) can call the constructors of all parent classes.










share|improve this question




















  • 1




    Base class constructors always needs to be called, how would the base classes otherwise construct (and possibly initialize) their member variables? Besides, if the problem was related to the "abstractness" of the classes, the error would probably be different. And please try to create a Minimal, Complete, and Verifiable Example to show us, and include the full and complete copy-paste of the compiler output.
    – Some programmer dude
    Nov 15 at 1:47








  • 2




    If I hack the code provided to make it compile (!), I can't reproduce this problem. So, Minimal, Complete, and Verifiable example please. It's certainly nothing to do with abstract classes.
    – Paul Sanders
    Nov 15 at 1:54








  • 1




    What's Vector? Does it have a default constructor?
    – songyuanyao
    Nov 15 at 1:58






  • 1




    The factors that forced the compiler to delete the default constructor are the factors that will also interfere with a naive manual implementation of that constructor. Judging by your current confusion, these factors will likely be just as surprising for you in that context as well. You are not providing enough information for us to figure out what these factors are. What is Vector?
    – AnT
    Nov 15 at 2:01








  • 2




    Is that the entire error message? Usually the compiler produces a longer explanation of why that constructor is deleted.
    – Pete Becker
    Nov 15 at 2:21















up vote
-3
down vote

favorite












I'm trying to make a UI, and I want objects to inherit properties. The way I do this is I have a templated class Widget which takes a variable amount of types that it inherits from (see code below).



However, when I try to make a class that inherits from Widget<Resizeable> I get an error: The default constructor of Widget<Resizeable> is deleted.



Could someone explain why this happens? Widget and Resizeable are both abstract, so why is the constructor being called?



Widget:



template<typename... B>
class Widget : public B...
{
public:
virtual void Move(Vector position) = 0;
virtual void Show(bool show) = 0;

template<typename T>
bool HasBehavior()
{
return std::is_base_of<T, std::remove_reference<decltype(*this)>::type>::value;
}
protected:
// protected members
};


Resizeable:



class Resizeble
{
public:
virtual void Resize(Vector size) = 0;

Vector Size() const noexcept
{
return size_;
}
protected:
Vector size_;
};


Rectangle:



class Rectangle : public Widget<Resizeble>
{
public:
Rectangle(Vector position, Vector size, Color color) : color_(color)
{

}
private:
Color color_;
};


SOLUTION:



Implement constructors for all base classes, and use virtual inheritance so that the most derived class (in this case, Rectangle) can call the constructors of all parent classes.










share|improve this question




















  • 1




    Base class constructors always needs to be called, how would the base classes otherwise construct (and possibly initialize) their member variables? Besides, if the problem was related to the "abstractness" of the classes, the error would probably be different. And please try to create a Minimal, Complete, and Verifiable Example to show us, and include the full and complete copy-paste of the compiler output.
    – Some programmer dude
    Nov 15 at 1:47








  • 2




    If I hack the code provided to make it compile (!), I can't reproduce this problem. So, Minimal, Complete, and Verifiable example please. It's certainly nothing to do with abstract classes.
    – Paul Sanders
    Nov 15 at 1:54








  • 1




    What's Vector? Does it have a default constructor?
    – songyuanyao
    Nov 15 at 1:58






  • 1




    The factors that forced the compiler to delete the default constructor are the factors that will also interfere with a naive manual implementation of that constructor. Judging by your current confusion, these factors will likely be just as surprising for you in that context as well. You are not providing enough information for us to figure out what these factors are. What is Vector?
    – AnT
    Nov 15 at 2:01








  • 2




    Is that the entire error message? Usually the compiler produces a longer explanation of why that constructor is deleted.
    – Pete Becker
    Nov 15 at 2:21













up vote
-3
down vote

favorite









up vote
-3
down vote

favorite











I'm trying to make a UI, and I want objects to inherit properties. The way I do this is I have a templated class Widget which takes a variable amount of types that it inherits from (see code below).



However, when I try to make a class that inherits from Widget<Resizeable> I get an error: The default constructor of Widget<Resizeable> is deleted.



Could someone explain why this happens? Widget and Resizeable are both abstract, so why is the constructor being called?



Widget:



template<typename... B>
class Widget : public B...
{
public:
virtual void Move(Vector position) = 0;
virtual void Show(bool show) = 0;

template<typename T>
bool HasBehavior()
{
return std::is_base_of<T, std::remove_reference<decltype(*this)>::type>::value;
}
protected:
// protected members
};


Resizeable:



class Resizeble
{
public:
virtual void Resize(Vector size) = 0;

Vector Size() const noexcept
{
return size_;
}
protected:
Vector size_;
};


Rectangle:



class Rectangle : public Widget<Resizeble>
{
public:
Rectangle(Vector position, Vector size, Color color) : color_(color)
{

}
private:
Color color_;
};


SOLUTION:



Implement constructors for all base classes, and use virtual inheritance so that the most derived class (in this case, Rectangle) can call the constructors of all parent classes.










share|improve this question















I'm trying to make a UI, and I want objects to inherit properties. The way I do this is I have a templated class Widget which takes a variable amount of types that it inherits from (see code below).



However, when I try to make a class that inherits from Widget<Resizeable> I get an error: The default constructor of Widget<Resizeable> is deleted.



Could someone explain why this happens? Widget and Resizeable are both abstract, so why is the constructor being called?



Widget:



template<typename... B>
class Widget : public B...
{
public:
virtual void Move(Vector position) = 0;
virtual void Show(bool show) = 0;

template<typename T>
bool HasBehavior()
{
return std::is_base_of<T, std::remove_reference<decltype(*this)>::type>::value;
}
protected:
// protected members
};


Resizeable:



class Resizeble
{
public:
virtual void Resize(Vector size) = 0;

Vector Size() const noexcept
{
return size_;
}
protected:
Vector size_;
};


Rectangle:



class Rectangle : public Widget<Resizeble>
{
public:
Rectangle(Vector position, Vector size, Color color) : color_(color)
{

}
private:
Color color_;
};


SOLUTION:



Implement constructors for all base classes, and use virtual inheritance so that the most derived class (in this case, Rectangle) can call the constructors of all parent classes.







c++ class visual-c++ constructor virtual






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 at 9:12









TrebuchetMS

1,6071618




1,6071618










asked Nov 15 at 1:43









Krystian S

1719




1719








  • 1




    Base class constructors always needs to be called, how would the base classes otherwise construct (and possibly initialize) their member variables? Besides, if the problem was related to the "abstractness" of the classes, the error would probably be different. And please try to create a Minimal, Complete, and Verifiable Example to show us, and include the full and complete copy-paste of the compiler output.
    – Some programmer dude
    Nov 15 at 1:47








  • 2




    If I hack the code provided to make it compile (!), I can't reproduce this problem. So, Minimal, Complete, and Verifiable example please. It's certainly nothing to do with abstract classes.
    – Paul Sanders
    Nov 15 at 1:54








  • 1




    What's Vector? Does it have a default constructor?
    – songyuanyao
    Nov 15 at 1:58






  • 1




    The factors that forced the compiler to delete the default constructor are the factors that will also interfere with a naive manual implementation of that constructor. Judging by your current confusion, these factors will likely be just as surprising for you in that context as well. You are not providing enough information for us to figure out what these factors are. What is Vector?
    – AnT
    Nov 15 at 2:01








  • 2




    Is that the entire error message? Usually the compiler produces a longer explanation of why that constructor is deleted.
    – Pete Becker
    Nov 15 at 2:21














  • 1




    Base class constructors always needs to be called, how would the base classes otherwise construct (and possibly initialize) their member variables? Besides, if the problem was related to the "abstractness" of the classes, the error would probably be different. And please try to create a Minimal, Complete, and Verifiable Example to show us, and include the full and complete copy-paste of the compiler output.
    – Some programmer dude
    Nov 15 at 1:47








  • 2




    If I hack the code provided to make it compile (!), I can't reproduce this problem. So, Minimal, Complete, and Verifiable example please. It's certainly nothing to do with abstract classes.
    – Paul Sanders
    Nov 15 at 1:54








  • 1




    What's Vector? Does it have a default constructor?
    – songyuanyao
    Nov 15 at 1:58






  • 1




    The factors that forced the compiler to delete the default constructor are the factors that will also interfere with a naive manual implementation of that constructor. Judging by your current confusion, these factors will likely be just as surprising for you in that context as well. You are not providing enough information for us to figure out what these factors are. What is Vector?
    – AnT
    Nov 15 at 2:01








  • 2




    Is that the entire error message? Usually the compiler produces a longer explanation of why that constructor is deleted.
    – Pete Becker
    Nov 15 at 2:21








1




1




Base class constructors always needs to be called, how would the base classes otherwise construct (and possibly initialize) their member variables? Besides, if the problem was related to the "abstractness" of the classes, the error would probably be different. And please try to create a Minimal, Complete, and Verifiable Example to show us, and include the full and complete copy-paste of the compiler output.
– Some programmer dude
Nov 15 at 1:47






Base class constructors always needs to be called, how would the base classes otherwise construct (and possibly initialize) their member variables? Besides, if the problem was related to the "abstractness" of the classes, the error would probably be different. And please try to create a Minimal, Complete, and Verifiable Example to show us, and include the full and complete copy-paste of the compiler output.
– Some programmer dude
Nov 15 at 1:47






2




2




If I hack the code provided to make it compile (!), I can't reproduce this problem. So, Minimal, Complete, and Verifiable example please. It's certainly nothing to do with abstract classes.
– Paul Sanders
Nov 15 at 1:54






If I hack the code provided to make it compile (!), I can't reproduce this problem. So, Minimal, Complete, and Verifiable example please. It's certainly nothing to do with abstract classes.
– Paul Sanders
Nov 15 at 1:54






1




1




What's Vector? Does it have a default constructor?
– songyuanyao
Nov 15 at 1:58




What's Vector? Does it have a default constructor?
– songyuanyao
Nov 15 at 1:58




1




1




The factors that forced the compiler to delete the default constructor are the factors that will also interfere with a naive manual implementation of that constructor. Judging by your current confusion, these factors will likely be just as surprising for you in that context as well. You are not providing enough information for us to figure out what these factors are. What is Vector?
– AnT
Nov 15 at 2:01






The factors that forced the compiler to delete the default constructor are the factors that will also interfere with a naive manual implementation of that constructor. Judging by your current confusion, these factors will likely be just as surprising for you in that context as well. You are not providing enough information for us to figure out what these factors are. What is Vector?
– AnT
Nov 15 at 2:01






2




2




Is that the entire error message? Usually the compiler produces a longer explanation of why that constructor is deleted.
– Pete Becker
Nov 15 at 2:21




Is that the entire error message? Usually the compiler produces a longer explanation of why that constructor is deleted.
– Pete Becker
Nov 15 at 2:21

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53311277%2fconstructor-call-to-pure-virtual-class-is-deleted%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53311277%2fconstructor-call-to-pure-virtual-class-is-deleted%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Biblatex bibliography style without URLs when DOI exists (in Overleaf with Zotero bibliography)

ComboBox Display Member on multiple fields

Is it possible to collect Nectar points via Trainline?