syntax error in exercise about class
I have to make a program where a disc is being moved on a 10 * 10 grid starting at position (1,1).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
|
#include <iostream>
using namespace std;
class Grid{
private:
short x = 1, y = 1, across, down;
public:
void getCoordinates();
void calcCoordinates(){x += across; y += down;};
void outCoordinates(){cout << "Disc possition: (" << x << "," << y << ")\n\n";};
};
int main(){
do{
Grid disc;
disc.getCoordinates();
disc.calcCoordinates();
disc.outCoordinates();
}
while(x != 10 && y != 10);
return 0;
}
void Grid::getCoordinates(){
bool correct;
do{
bool correct = true;
cout << "\n\tMove across: ";
cin >> across;
if ((x + across) < 0 || (x + across) > 10)
correct = false;
}
while(!correct);
do{
bool correct = true;
cout << "\n\tMove down: ";
cin >> down;
if((y + down) < 0 || (y + down) > 10)
correct = false;
}
while(!correct);
}
|
how ever I get an error in line 22: 'x' and 'y' was not declared in this scope.
moving it to public doesn't change anything.
any idea why is that?
Where did you declare x or y variable in your main function?
in the class Grid, line 8
You do not refer to variable in class, you refer to variable in current scope.
Consider
1 2 3 4
|
Grid disc;
Grid disc2;
//Which x? How can you refer unambiguously to correct one?
//x = 5
|
i get your point, i just had one class on Classes. how do i distinguish between the x i want and some other x?
disc.x = 5;
Topic archived. No new replies allowed.