C++ 没有匹配的函数用于调用。。。继承的C+中缺少重载+;班

C++ 没有匹配的函数用于调用。。。继承的C+中缺少重载+;班,c++,inheritance,overloading,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.c

有人能解释一下这是怎么回事吗。为什么编译器在类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

B
中的
hello(int)
成员函数接受int参数并隐藏
A
hello()
成员。如果希望
B
hello
成员函数不隐藏
A
中的函数,请使用A::hello将
添加到
B
,因为覆盖
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) {}
};

在派生类中声明名为
hello
的函数将隐藏基类中具有相同名称的所有函数。您可以使用using声明取消隐藏它们:

struct B : A {
    using A::hello;
    virtual void hello(int arg) {}
};
或通过基类指针或引用访问它们:

static_cast<A*>(b)->hello();
static_cast(b)->hello();

Required参数,读取编译器给UBE的内容,因为只考虑继承名称,而不考虑完整原型。如果派生类中已有基类,则不会考虑基类1,除非添加using语句将其拉入。