Arduino - 在 setup() 中声明的变量不在函数范围内
Arduino - Variables declared in setup() not in scope of function
我计划编写一个修改二维数组的函数,以便每个坐标都设置为 0。在 setup()
中,我声明了 displayWidth
和 displayHeight
但不能'不要在 generateBoard()
函数中访问它们,因为它们不在同一范围内。
代码:
void generateBoard(int board[][]) {
// Modifies the array board by setting zeros
for (int y=0; y < displayHeight; y++) {
for (int x=0; x < displayWidth; x++) {
board[x][y] = 0;
}
}
}
void setup() {
int displayWidth = 14;
int displayHeight = 10;
int board[displayWidth][displayHeight];
generateBoard(board);
}
void loop() {}
setup() 内的局部范围异常
error: declaration of 'board' as multidimensional array must have bounds for all dimensions except the first
error: declaration of 'board' as multidimensional array must have bounds for all dimensions except the first
In function 'void generateBoard(...)':
error: 'displayHheight' was not declared in this scope
error: 'displayWidth' was not declared in this scope
error: 'board' was not declared in this scope
固定和工作版本:
const int displayWidth = 14;
const int displayHeight = 10;
int board[displayWidth][displayHeight];
void generateBoard() {
// Modifies the array board by setting zeros
for (int y=0; y < displayHeight; y++) {
for (int x=0; x < displayWidth; x++) {
board[x][y] = 0;
}
}
}
void setup() {
generateBoard();
}
void loop(){}
全局声明 board
、displayWidth
和 displayHeight
(在任何函数定义之外)。像这样:
const int displayWidth = 14;
const int displayHeight = 10;
int board[displayWidth][displayHeight];
void generateBoard() {
// Modifies the array board by setting 0
for (int y=0; y < displayHeight; y++) {
for (int x=0; x < displayWidth; x++) {
board[x][y] = 0;
}
}
}
void setup() {
generateBoard();
}
void loop() {}
在 setup() 中声明它们会使它们成为局部变量 - 局部变量只能由声明它们的函数访问。
我计划编写一个修改二维数组的函数,以便每个坐标都设置为 0。在 setup()
中,我声明了 displayWidth
和 displayHeight
但不能'不要在 generateBoard()
函数中访问它们,因为它们不在同一范围内。
代码:
void generateBoard(int board[][]) {
// Modifies the array board by setting zeros
for (int y=0; y < displayHeight; y++) {
for (int x=0; x < displayWidth; x++) {
board[x][y] = 0;
}
}
}
void setup() {
int displayWidth = 14;
int displayHeight = 10;
int board[displayWidth][displayHeight];
generateBoard(board);
}
void loop() {}
setup() 内的局部范围异常
error: declaration of 'board' as multidimensional array must have bounds for all dimensions except the first
error: declaration of 'board' as multidimensional array must have bounds for all dimensions except the first
In function 'void generateBoard(...)':
error: 'displayHheight' was not declared in this scope
error: 'displayWidth' was not declared in this scope
error: 'board' was not declared in this scope
固定和工作版本:
const int displayWidth = 14;
const int displayHeight = 10;
int board[displayWidth][displayHeight];
void generateBoard() {
// Modifies the array board by setting zeros
for (int y=0; y < displayHeight; y++) {
for (int x=0; x < displayWidth; x++) {
board[x][y] = 0;
}
}
}
void setup() {
generateBoard();
}
void loop(){}
全局声明 board
、displayWidth
和 displayHeight
(在任何函数定义之外)。像这样:
const int displayWidth = 14;
const int displayHeight = 10;
int board[displayWidth][displayHeight];
void generateBoard() {
// Modifies the array board by setting 0
for (int y=0; y < displayHeight; y++) {
for (int x=0; x < displayWidth; x++) {
board[x][y] = 0;
}
}
}
void setup() {
generateBoard();
}
void loop() {}
在 setup() 中声明它们会使它们成为局部变量 - 局部变量只能由声明它们的函数访问。