【问题标题】:‘double& Point::operator[](unsigned int)’ is inaccessible within this context'double& Point::operator[](unsigned int)' 在此上下文中不可访问
【发布时间】:2012-06-26 18:49:48
【问题描述】:

我有两个类设置如下:

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

为什么?

【问题讨论】:

  • class Vector : **public** Point
  • 我猜应该是class Vector: public Point

标签: c++ inheritance compiler-errors operator-overloading


【解决方案1】:

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

class Vector : public Point { // public

public:
    Vector(double x, double y, double z);
    ~Vector() {};
    double norm();
    void normalize();
};

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-24
      • 2012-08-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多