暧昧类继承(Ambiguous class inheritance)

2019-09-23 17:46发布

#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;
}

但问题是:

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

在成员函数'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()

Answer 1:

有没有必要推导TcilindroTcirculo ,如果从产生它是足够Trectangulo



Answer 2:

因为相同的基类继承乘法的这些问题。 在您的示例中class Tcilindro继承自TrectanguloTcirculoTrectangulo已经衍生TcirculoTcilindro具有相同功能的双重定义。 你只需要省略Tcirculo这里类删除继承功能歧义。



文章来源: Ambiguous class inheritance