Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot find baseclass function if baseclass has two functions of same name

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?

like image 722
Benjamin Bannier Avatar asked Jan 19 '11 23:01

Benjamin Bannier


1 Answers

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.

like image 183
Lightness Races in Orbit Avatar answered Sep 19 '22 23:09

Lightness Races in Orbit