【发布时间】:2016-03-24 00:40:33
【问题描述】:
我正在阅读Inheriting constructors here 上的文档。有一个例子:
struct B1 {
B1(int);
};
struct D1 : B1 {
using B1::B1;
// The set of inherited constructors is
// 1. B1(const B1&)
// 2. B1(B1&&)
// 3. B1(int)
// D1 has the following constructors:
// 1. D1()
// 2. D1(const D1&)
// 3. D1(D1&&)
// 4. D1(int) <- inherited
};
所以写得很清楚D1 has the following constructors: D1(),即默认构造函数。但是当我试图创建一个对象时:
D1 d;
我有一个错误use of deleted function 'D1::D1()'。是文档中的错误还是我误解了什么?
我用 c++14 尝试了 gcc。
【问题讨论】:
-
@POTEMKINDX 您忘记在
main()中添加D1 d;。如果你这样做了,你会发现它不起作用:coliru.stacked-crooked.com/a/220ea673061325fd -
@POTEMKINDX 你没有调用导致error的构造函数
-
@POTEMKINDX 已更新。顺便说一句,您没有创建实例。
-
@nikitablack 他们说
D1()存在于他们拥有的下一个代码块中的代码块下方D1 e; // Error: D1 has no default constructor -
@KerrekSB 问题是 cppreference 上的第一个示例说它应该在该示例中具有默认值。
标签: c++ inheritance constructor default-constructor