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
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) {}
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With