-1

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();
}

2 Answers2

1

In A.cpp it should be

void A::doThings(C* c){}

Also I believe your C class should define that method as protected, so that derived classes can access it.

saykou
  • 167
  • 1
  • 9
  • 1
    Virtual inheritance has nothing to do with having to use base class qualification. And why should `B`, but not `C`, be used as a virtual base class? – Angew is no longer proud of SO Mar 13 '17 at 11:32
  • you are right, it should be C. Your are also right, since virtual is already defined in the base method there is no need for a base class qualifier. Will change my answer. – saykou Mar 13 '17 at 15:15
  • All the involved methods are public, I edited my code. Also I found the reason for the ambiguity, I added my answer below – ellysisland Mar 13 '17 at 15:38
0

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!
}