I have the following code:
class Point{
private:
double x, y;
public:
Point(){
this->x = 0;
this->y = 0;
}
//...
};
I am getting an error on the private declaration of doubles x and y:
'double Point::y' is private
My code works fine when I rearrange it as:
class Point{
//private:
public:
double x, y;
//public:
Point(){
this->x = 0;
this->y = 0;
}
//...
};
But obviously then double isn't private. If no private keyword is given and the doubles are declared before the public keyword, I get the same private variable error.
What am I doing wrong / how can I declare private variables properly?
Thanks!
I have error makers in eclipse both within the class and at the declaration of the doubles: (I placed an X in the code since I can't post screen shots)
class Point{
private:
//public:
X double x, y;
X Point(): x{0},y{0}{
}
SOLVED: Thanks to Dieter Lücking for point out that in my example I forgot to uncomment the public keyword when I provided a full code example. Further along in my code I had written:
cout << "\nPoint sum: " << e.x + e.getY();
This, oddly, caused errors to appear both on the cout line, as well as the declaration line, which is what caused my confusion. Fixing the cout line:
cout << "\nPoint sum: " << e.x + e.getY();
Solved all the errors. Thank you all!