0
votes
class base
{
  public:
     virtual void display() = 0;
};

class derived : virtual public base
{
 public:
    void display()
    {
        cout << "Display of derived : " << std::endl;
    }
};

class derived1 : virtual public base
{
  public:
    void display()
    {
        cout << "Display of derived : " << std::endl;
    }
};

class derived2 : public derived, derived1
{

};

I am taking one pure virtual function into base class. I am using virtual keyword while creating derived and derived1 class which is inherited from my base class, and I finally created derived2 class which inherit from derived and derived1 ,Then I will get error "derived2:ambigous inheritance of base::void(display)" How to resolve this error ?

1
provide void derived2::display(void)Andrew Kashpur
How can the compiler decide which display to use between the two if you don't tell?Matthieu Brucher
Well, you've solved the diamond problem. What you have is just a multiple inheritance problem now.DeiDei
A pig says "oink, oink". A dog says "woof, woof". Based on this information, can you tell what a pigdog says?n. 1.8e9-where's-my-share m.
Nitpick: either you have using namespace std; and you don't use std:: qualification anywhere, or you use it everywhere!curiousguy

1 Answers

4
votes

You need to decide which of the two is the derived method, since both derived and derived1 provide an implementation.

Using non-virtual functions, the solution would be more straightforward: by simply writing using derived::display or using derived1::display

But you're using virtual functions, so you will need to add a final overriding function. It can be done like so:

class derived2 : public derived, derived1 {
  public:
    void display() override {
      derived::display(); // or derived1::display();
    }
}