mardi 15 septembre 2020

Multiple inheritance of abstract class results in ambiguous definition

I am facing inheritance hierarchy error which I root caused to this code snippet. The definition of l() is defined in Template class Y, I don't understand why is the compiler finding it ambiguous.Class Z ptr_ is pointing to class Y object which has the definition of l() isn't this how it should work ?

Error:
Line 84: Char 13: error: non-static member 'l' found in multiple base-class subobjects of type 'D':
    class C -> class A -> class D
    class C -> class B -> class D
      ptr_->l ()

Line 96: Char 15: note: in instantiation of member function 'Z<C>::~Z' requested here
  Z < C > t = new Y < C > ();
              ^

Line 31: Char 16: note: member found by ambiguous name lookup
  virtual void l () = 0;
#include <iostream>

using namespace std;

template <class T> 
class Y : public T {
public:
  void l() {}
  void p() {}
};

class D {
public:
  virtual void l() = 0;
  virtual void p() = 0;
};

class A : public D {
public:
  virtual void f(int a) = 0;
  virtual void d() = 0;
};

class B : public D {
public:
  virtual void j(string x) = 0;
  virtual void d() = 0;
};

class C : public A, public B {
public:
  void f(int a) override {}

  void j(string x) override {}

  void d() override {}
};

template <class T> 
class Z {
public:
  Z(T *ptr) : ptr_(ptr) {}

  ~Z() {
    if (ptr_)
      ptr_->l();
  }

protected:
  T *ptr_;
};

int main() {

  Z<C> t = new Y<C>();

  return 0;
}


Aucun commentaire:

Enregistrer un commentaire