由于歧义导致Direct Base无法访问 [英] Direct Base inaccessible in derived due to ambiguity

查看:673
本文介绍了由于歧义导致Direct Base无法访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试用于学习目的的实验计划。

考虑下面的继承

I am just trying out an experimental program for learning purpose.
Consider the inheritence below

      Base
  _____|
|      |
|      |
D1     |
|      |
|____ _|
       |
       D2




#include<iostream>
using namespace std;

class Base{
int bData;
public:Base(int i):bData(i)     //Base single parameter constructor
        {
        }
        
        Base():bData(0)         //Base Default constuctor
        {
        }        

        void display()
        {
          cout<<"bData = "<<bData<<endl;
        }
};

class D1:public Base{
int dData;
public:D1(int i,int j):Base(i),dData(j)  // D1 two parameter constructor
        {
        }
        
        D1(int i):dData(i)    //D1 single parameter constructor
        {
        }        
        
        void display()
        {
          Base::display();
          cout<<"dData = "<<dData<<endl;        
        }    
};

/*  //commented
class D2: virtual public D1,virtual public Base{    // --error   
int d2data;
public:D2(int i,int j,int k):D1(i),Base(j),d2data(k)
        {
        }
           
        void display()
        {
          D1::display();
          cout<<"d2data= "<<d2data<<endl;        
        }    

};
*/

int main()
{
D1 obj1(5,10);
obj1.display();
D1 obj2(3);
obj2.display();
//D2 obj3(7,8,9);     /* commented */
//obj3.display();     /* commented */
return 0;
}



上述程序的输出按预期输出:


The output of the above program gives the output as expected:

//----------------------------------------------------------
bData = 5           -- obj1
dData = 10          -- obj1
bData = 0           -- obj2
dData = 3           -- obj2
-------------------------------------------------------------



在取消注释上面的D2类时,我遇到以下错误


On uncommenting the class D2 above i come across the below error

cc1plus: warnings being treated as errors
warning: direct base 'Base' inaccessible in 'D2' due to ambiguity
warning: virtual base 'Base' inaccessible in 'D2' due to ambiguity



请建议我如何能够超越这个错误或者我如何以正确的方式做到这一点

我可以做出所需的基础D2中的类构造函数调用。



删除关键字virtual只显示错误


Please suggest how can i surpass this error or how do i do it in a proper way so
that i can make the desired base class constructor call in D2.

Removal of keyword virtual shows up only the error

warning: direct base 'Base' inaccessible in 'D2' due to ambiguity

推荐答案

你正面临他们所谓的'钻石问题 [ ^ ]'...

在排序D2中,也通过D1继承Base,也直接使编译器无法决定继承Base.display或D1.display的方法......

阅读上面的文章...

很多语言不支持这个并且没有解决方案,幸运的是C ++可以绕过虚拟继承...

请阅读本文如何做到这一点: http://www.cprogramming.com/tutorial/virtual_inheritance.html [ ^ ]
You are facing what they called the 'diamond problem[^]'...
In sort D2 inherits Base also via D1 and also directly so compiler can't decide what method to inherit Base.display or D1.display...
Read the article above...
Lot of languages do not support this and have no solution, fortunately C++ can go around sing virtual inheritance...
Please read this article how to do that: http://www.cprogramming.com/tutorial/virtual_inheritance.html[^]


我通过这个问题解决了gh使用虚拟基类。



I got through the problem through use of virtual base class.

#include<iostream>
using namespace std;
 
class Base{
int bData;
public:Base(int i):bData(i)     //Base single parameter constructor
        {
        }
        
        Base():bData(0)         //Base Default constuctor
        {
        }        
 
        void display()
        {
          cout<<"bData = "<<bData<<endl;
        }
       
};
 
class D1:virtual public Base{
int dData;
public:D1(int i,int j):Base(i),dData(j)  // D1 two parameter constructor
        {
        }
        
        D1(int i):dData(i)    //D1 single parameter constructor
        {
        }        
        
        void display()
        {
          Base::display();
          cout<<"dData = "<<dData<<endl;        
        }
        
};
 
  //commented
class D2: virtual public Base,virtual public D1{    // --error   
int d2data;
public:D2(int i,int j,int k):Base(i),D1(j),d2data(k)
        {
        }
           
        void display()
        {
          D1::display();
          cout<<"d2data= "<<d2data<<endl;        
        }    
       
};

 
int main()
{
D1 obj1(5,10);
obj1.display();
D1 obj2(3);
obj2.display();
D2 obj3(7,8,9);     
obj3.display();     
return 0;
}

--------------------------
Output:

bData = 5        -- obj1
dData = 10       -- obj1

bData = 0        -- obj2
dData = 3        -- obj2

bData = 7        -- obj3
dData = 8        -- obj3
d2data= 9        -- obj3


这篇关于由于歧义导致Direct Base无法访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆