c++ - arduino为多维数组赋值

标签 c++ arrays arduino

我有一个 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/

相关文章:

android - 与 Arduino + Android 的蓝牙通信

c++ - “过早的 EOF”和 'Bad Character' 错误

jquery - 如何使用jquery将序列化字符串转换为数组

c++ - Arduino 计算错误 - 在不应该的时候给出负值

python - 在 numpy 数组中查找负值和正值

php - 这个php代码有什么问题?

c++ - 发送字节数组的一部分

c++ - 多线程的 shared_ptr 销毁是否安全?

c++ - 当前正在运行哪个单元测试?

c++ - 尝试使用谷歌测试编译 main 时获取 "multiple definition of func_name"