5

我有两个类设置如下:

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();
};

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

Vector v;
printf("%d\n", v[0]);

我得到:

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

为什么?

4

2 回答 2

22

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

class Vector : public Point { // public

public:
    Vector(double x, double y, double z);
    ~Vector() {};
    double norm();
    void normalize();
};
于 2012-06-26T18:51:16.027 回答
0

类的默认继承说明符是私有的。

于 2012-06-26T18:51:13.793 回答