【问题标题】:Mixing overriding and overloading in C++ [duplicate]在 C++ 中混合覆盖和重载 [重复]
【发布时间】:2020-04-18 04:51:06
【问题描述】:

我不明白为什么在尝试编译代码时出现此错误error: no matching function for call to ‘Child::m(Mother&)

据我了解: 因为 c 的类型是 Child 并且 Child::m 有一个 Child 类型的参数,而 m 在 c.m(m) 中是 Mother 类型的,那么需要调用的是来自 Mother 类的函数 m() .

class Mother{
 public: 
  void m(const Mother&) {
  }
};

class Child: public Mother{
 public:
  void m(const Child&) {
  }
};



int main() {
  Mother m;
  Child c;
  c.m(m);
}

【问题讨论】:

    标签: c++ debugging computer-science


    【解决方案1】:

    您的Child 类没有成员函数m 接受Mother。它被Child 中声明的m 隐藏。您可以通过在Child 中添加using Mother::m; 来取消隐藏它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多