c++inheritanceambiguous-call

Ambiguous method compilation error


I have the following code involving three classes A,B,C File A.pp does not compile with error 'ambigous call' on the call to inheriTed method doWhat() Which is the cause of the problem? How do I avoid it?

A.h

#include "B.h
class A: public B, C{
 virtual void doThings(C* c);
}

 

A.cpp

void doThings(C* c){
   this->doWhat();    //Compilation  Error doWhat call is ambigous!
}

 

B.h

class C; //forward declaration 
class B{
    public:
    virtual void doThings(C* c) = 0;
}

 

C.h

#include "B.h"

class C{
public:
   virtual void doStuff(B* b);
   virtual void doWhat();
}

Solution

  • I found the reason for the ambiguity: Class A is also inheriting from class D, which was already a son of C, therefore the ambiguity on doWhat() call

    A.h
    
    #include "B.h"
    class A: public B, C, D{
       virtual void doThings(C* c);
    }
    
    D.h
    #include "C.h"
    class D:public C{
    
    }
    

    The problem was avoided removing the redundant inheritance declaration , modifying class A like following:

    A.h
    
    #include "B.h"
    #include "D.h"
    
    class A: public B, D{
    virtual void doThings(C* c);
    }
    
    
    A.cpp
    void doThings(C* c){
        this->doWhat();    //Now compiling!
    }