我设置了两个类,如下所示:
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
为什么?
答案 0 :(得分:15)
类继承默认是私有的。您必须明确告诉编译器您想要公共继承:
class Vector : public Point { // public
public:
Vector(double x, double y, double z);
~Vector() {};
double norm();
void normalize();
};
答案 1 :(得分:0)
类的默认继承说明符是私有的。