Ambiguous class inheritance

一世执手 提交于 2019-12-10 14:09:20

问题


#include <iostream>
#include <cmath>
using namespace std;
class Tcirculo{
        float radio;
        float diametro;
        float area;
public:
        void carea(float r){radio= r; area=(M_PI*((r*r)));}
        float cdiam(float r) {diametro = 2*r; return diametro;}
        float getr(){return radio;}
        float getd(){return diametro;}
        float geta(){return area;}

};


class Trectangulo : public Tcirculo{
        float altura;
public:
        float calca(float h, float r){altura =h;
        float arearec = getd() * h; return arearec;}
};

class Tcilindro :  public Tcirculo ,Trectangulo{
        float xx,bb;
public:
        Tcilindro(float a, float b) {xx=a;bb=b;}
        float area_total();
};

float Tcilindro::area_total(){
        int area;
        area = ((2*((getd())))+calca(bb,xx));
        return area;
}
int main(int argc, char *argv[]) {

        return 0;
}

but the problem is :

warning: direct base 'Tcirculo' inaccessible in 'Tcilindro' due to ambiguity

In member function 'float Tcilindro::area_total()':

error: reference to 'geta' is ambiguous
error: candidates are: float Tcirculo::geta()
error:                 float Tcirculo::geta()
error: reference to 'geta' is ambiguous
error: candidates are: float Tcirculo::geta()
error:                 float Tcirculo::geta()

回答1:


There is no need to derive Tcilindro from Tcirculo, it is sufficient if you derive it from Trectangulo.




回答2:


These problems because of multiply inheritance with same Base Class. In you example class Tcilindro inherits from Trectangulo and Tcirculo but Trectangulo already derived from Tcirculo and Tcilindro have double definition of same functions. You just need to omit Tcirculo class here to remove ambiguity of inherited functions.



来源:https://stackoverflow.com/questions/12793651/ambiguous-class-inheritance

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!