相关疑难解决方法(0)

为什么“继承的构造函数不是从相同或派生类型的表达式初始化的候选者”?

我希望这能奏效

struct Parent
{
    Parent ();

    Parent (const Parent &);
};

struct Child : public Parent
{
    using Parent::Parent;
};

Parent p;

Child c (p);
Run Code Online (Sandbox Code Playgroud)

Child继承了所有Parent的构造函数,对吧?

包括Parent::Parent(const Parent &)

x.cpp:15:11: error: no matching function for call to ‘Child::Child(Parent&)’
 Child c (p);
           ^
x.cpp:5:2: note: candidate: Parent::Parent(const Parent&)
  Parent (const Parent &);
  ^~~~~~
x.cpp:10:16: note:   inherited here
  using Parent::Parent;
                ^~~~~~
x.cpp:10:16: note:   an inherited constructor is not a candidate for initialization from an expression of the …
Run Code Online (Sandbox Code Playgroud)

c++ inheritance constructor c++11

11
推荐指数
2
解决办法
702
查看次数

标签 统计

c++ ×1

c++11 ×1

constructor ×1

inheritance ×1