0

Why does the compiler not find the base class function signature? Changing foo( a1 ) to B::foo( a1 ) works.

Code:

class A1 ;
class A2 ;

class B
{
public:
   void foo( A1* a1 ) { a1 = 0 ; }
} ;

class C : public B
{
public:
   void foo( A2* /*a2*/ )
   {
      A1* a1 = 0 ;
      foo( a1 ) ;
   }
} ;

int main()
{
   A2* a2 = 0 ;
   C c ;
   c.foo( a2 ) ;
   return 0 ;
}

Compiler error (VS2008):

error C2664: 'C::foo' : cannot convert parameter 1 from 'A1 *' to 'A2 *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
swongu
  • 2,229
  • 3
  • 19
  • 24

1 Answers1

5

The name C::foo shadows the name B::foo. Once the compiler finds the matching foo in class C, it stops searching any further.

You can resolve your problem by adding:

using B::foo;

to the body of class C, or by renaming the function in class B.

James McNellis
  • 348,265
  • 75
  • 913
  • 977