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
4

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Also, main is not allowed to be void. It must return an int, by C++ standard.
You can also make the function virtual in class B.
My question now adheres to the C++ standard.
@ChrisInEdmonton: Making the functions virtual won't resolve this problem because the two declarations of foo have different signatures.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.