views:

161

answers:

1

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
+5  A: 

The name C::foo hides the name B::foo. Once the compiler finds the matching foo in class B, 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
Also, main is not allowed to be void. It must return an int, by C++ standard.
ChrisInEdmonton
You can also make the function virtual in class B.
ChrisInEdmonton
My question now adheres to the C++ standard.
swongu
@ChrisInEdmonton: Making the functions virtual won't resolve this problem because the two declarations of `foo` have different signatures.
James McNellis