如何在下面的代碼中使用另一個函數中的lastLoc對象的x和y值。我沒有得到任何錯誤,但是當我在getPosLoc功能打印lastLoc的價值觀,我得到一個較長的數字(可能是地址):如何在另一個函數中使用類對象? C++
class solveMaze {
private:
maze maze;
mouse m;
stack<coords> coordStack;
int x;
int y;
int posLocCount = 0;
coords lastLoc;
};
solveMaze::solveMaze() {
x = m.x;
y = m.y;
coords c(x, y);
coords lastLoc(c.x, c.y);
coordStack.push(c);
}
void solveMaze::getPosLoc() {
if((mazeLayout[x][y-1] == 0) && (x != lastLoc.x) && (y-1 != lastLoc.y)) {
posLocCount++;
putUp();
}
這是去掉無關的功能coords.h縮短代碼:
class coords {
public:
coords(){};
coords(int, int);
int x;
int y;
friend ostream &operator<<(ostream &output, const coords &c);
bool operator==(coords);
void operator=(const coords &b);
};
coords::coords(int a, int b) {
x = a;
y = b;
}
這是mouse.h:
class mouse {
private:
maze maze;
public:
mouse();
int x;
int y;
};
mouse::mouse() {
for (int i=0; i<12; i++) {
for (int j=0; j<29; j++) {
if (mazeLayout[i][j] == 8){
x = j;
y = i;
}
}
}
}
'x = m.x;'< - 我沒有看到你在該行之前初始化'm'。什麼是默認構建的'鼠標'應該包含? – Michael