I have a baseclass that has two functions of same name but with different signatures in a 2-level inheritance.
struct A {
virtual void f(int) { }
virtual void f(int, int) { };
virtual void f1(int) { }
};
struct B: public A { };
struct C: public B {
void f(int, int) { }
void f1(int) { }
};
int main() {
C obj;
obj.f1(0);
obj.f(0,0);
obj.f(0); // (1) cannot be found
obj.B::f(0); // (2) works
}
I would have expected my compiler (gcc-4.3.2) to find the correct definition at (1)
, but I get
g++ main.cpp -o main
main.cpp: In function 'int main()':
main.cpp:20: error: no matching function for call to 'C::f(int)'
main.cpp:10: note: candidates are: virtual void C::f(int, int)
distcc[2200] ERROR: compile main.cpp on localhost failed
make: *** [main] Error 1
(2)
on the other hand works.
What do I need to fix to make (1)
work in general?
Write using A::f
inside the definition of C.
You are a victim of name hiding! void C::f(int, int)
hides void A::f(int)
, just because.
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