'double&Point :: operator [](unsigned int)'在此上下文中不可访问

hou*_*oft 1 c++ inheritance compiler-errors operator-overloading

我有两个课程设置如下:

class Point {
protected:
    double coords[3];

public:
    Point(double x, double y, double z) {
        setX(x);
        setY(y);
        setZ(z);
    };
    ~Point() {};
    double x() {return coords[0];};
    double y() {return coords[1];};
    double z() {return coords[2];};
    void setX(double x) {
        coords[0] = x;
    };
    void setY(double y) {
        coords[1] = y;
    };
    void setZ(double z) {
        coords[2] = z;
    };
    double &operator[](unsigned int x) {
        return coords[x];
    }
};


class Vector:Point {

public:
    Vector(double x, double y, double z);
    ~Vector() {};
    double norm();
    void normalize();
};
Run Code Online (Sandbox Code Playgroud)

现在每当我尝试做类似的事情:

Vector v;
printf("%d\n", v[0]);
Run Code Online (Sandbox Code Playgroud)

我明白了:

error: ‘Point’ is not an accessible base of ‘Vector’
error: ‘double& Point::operator[](unsigned int)’ is inaccessible
error: within this context
Run Code Online (Sandbox Code Playgroud)

为什么?

mfo*_*ini 16

类继承默认是私有的.您必须明确告诉编译器您想要公共继承:

class Vector : public Point { // public

public:
    Vector(double x, double y, double z);
    ~Vector() {};
    double norm();
    void normalize();
};
Run Code Online (Sandbox Code Playgroud)