c++ - 没有用于调用的匹配函数...继承的 C++ 类中缺少重载

标签 c++ inheritance overloading

<分区>

谁能解释一下这是怎么回事。为什么编译器看不到类 A 中没有参数的 hello()?

struct A {
    virtual void hello() {}
    virtual void hello(int arg) {}
};
struct B : A {
    virtual void hello(int arg) {}
};

int main()
{
    B* b = new B();
    b->hello();
    return 0;
}

g++ main.cpp

main.cpp: In function ‘int main()’:
main.cpp:13:11: error: no matching function for call to ‘B::hello()’
main.cpp:13:11: note: candidate is:
main.cpp:7:15: note: virtual void B::hello(int)
main.cpp:7:15: note:   candidate expects 1 argument, 0 provided

最佳答案

因为您重写了 hello(int arg) 隐藏了其他同名函数。

你可以做的是显式地将那些基类函数引入子类:

struct B : A {
    using A::hello; // Make other overloaded version of hello() to show up in subclass.
    virtual void hello(int arg) {}
};

关于c++ - 没有用于调用的匹配函数...继承的 C++ 类中缺少重载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18253563/

相关文章:

java - 未定义的构造函数(java)

c# - 在哪里重载除法返回自定义类 C# operator/(int,int)

vb.net - VB.NET无法区分重载函数

c++ - C++比较字符串的字符

C++ 多重继承、基类可见性和可怕的菱形。将祖先基类重新公开为公共(public)?

c++ - store(x,std::memory_order_relaxed)与直接分配值之间的区别

c++ - 抽象类和派生类的输出运算符(<<)

c++ - 为什么不允许仅对一个 ref-qualifier 进行重载?

c++ - "C++ Concurrency in Action"中的阿姆达尔定律

c++ - 方法定义中的变量名称是否必须与 C++ 中的声明名称相同?