c++ - Private Variable Compile Error: Variable is Private -
i have following code:
class point{ private: double x, y; public: point(){ this->x = 0; this->y = 0; } //... };
i getting error on private declaration of doubles x , y:
'double point::y' private
my code works fine when rearrange as:
class point{ //private: public: double x, y; //public: point(){ this->x = 0; this->y = 0; } //... };
but double isn't private. if no private keyword given , doubles declared before public keyword, same private variable error.
what doing wrong / how can declare private variables properly? thanks!
i have error makers in eclipse both within class , @ declaration of doubles: (i placed x in code since can't post screen shots)
class point{ private: //public: x double x, y; x point(): x{0},y{0}{ }
solved: dieter lücking point out in example forgot uncomment public keyword when provided full code example. further along in code had written:
cout << "\npoint sum: " << e.x + e.gety();
this, oddly, caused errors appear both on cout line, declaration line, caused confusion. fixing cout line:
cout << "\npoint sum: " << e.x + e.gety();
solved errors. thank all!
the important thing notice in main
function try access x
, y
directly instead of using accessor functions getx()
, gety()
.
another thing (probably due recent editing) of member functions in point
private
. uncomment //public:
line directly before c'tor.
Comments
Post a Comment