我有一个 arduino 项目,它使用二维数组来跟踪行/列信息。如果我尝试将行、列对分配给特定元素,则会收到错误:
expected primary-expression before '{' token
我可以毫无问题地分配单个值,但如果我尝试将第二个数组元素分配为整数对,则会出现错误。
example:
player[0][0] = 1;
player[0][1] = 2; //this works
//but . . .
player[0] = {1,2}; //doesn't work
//here is more specific code from the project
// …. a bunch of code here declaring various global variables
int players[6][2]; //two dimensional array with 6 players each with a 2 value array (column and row)
int score1; //player 1 score
int score2; //player2 score
//code here for setup/loop/etc.. Code calls the setPlayerPositions() function
void setPlayerPositions()
{
players[0] = {2,1}; //set the position of the ball (player[0]) to row 2, column 1
players[1] = {1,4}; //set other player positions . . .
players[2] = {2,4};
players[3] = {3,4};
players[4] = {2,6};
players[5] = {2,9};
}
最佳答案
我曾多次因为这个问题而头晕目眩。
不幸的是,初始化数组时只能使用大括号表示法。初始化后,您必须单独处理每个数组元素。
引用:Arduino 食谱... http://books.google.co.uk/books?id=nxxKNCYXRIwC&lpg=PA31&ots=dH_fWczOAp&pg=PA31#v=onepage
关于c++ - arduino为多维数组赋值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22289386/