Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

‘double& Point::operator[](unsigned int)’ is inaccessible within this context

I have two classes set up as follows:

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

Now whenever I try to do something like:

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

I get:

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

Why?

like image 997
houbysoft Avatar asked Jun 26 '12 18:06

houbysoft


People also ask

What is mean of double?

twice as large, heavy, strong, etc.; twofold in size, amount, number, extent, etc.: a double portion; a new house double the size of the old one. composed of two like parts or members; twofold in form; paired: double doors; a double sink. of, relating to, or suitable for two persons: a double room.


1 Answers

Class inheritance is by default private. You must explicitly tell the compiler that you want public inheritance:

class Vector : public Point { // public

public:
    Vector(double x, double y, double z);
    ~Vector() {};
    double norm();
    void normalize();
};
like image 112
mfontanini Avatar answered Oct 04 '22 22:10

mfontanini