Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

no matching function for call to... missing overload in inherited C++ class [duplicate]

Can someone please explain what's going on here. Why can't the compiler see hello() with no arguments in class A?

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
like image 733
jozxyqk Avatar asked Aug 15 '13 13:08

jozxyqk


1 Answers

Because your overriding of hello(int arg) hides other functions with the same name.

What you can do is to explicitly introduce those base class functions to subclass:

struct B : A {
    using A::hello; // Make other overloaded version of hello() to show up in subclass.
    virtual void hello(int arg) {}
};
like image 115
Eric Z Avatar answered Nov 14 '22 23:11

Eric Z