简体   繁体   English

错误:从初始化列表 C++ 分配给数组

[英]error:assigning to an array from an initializer list c++

I have declared a global multidimensional array int possw[8][2] then I called a function to make changes to it by using another array which I initialized int w[2]我已经声明了一个全局多维数组int possw[8][2]然后我调用了一个函数来使用另一个我初始化的数组来对其进行更改int w[2]

possw[8][2] = {{b[0]+1,b[1]+2},{b[0]+2,b[1]+1},{b[0]+2,b[1]-1},{b[0]+1,b[1]-2},{b[0]-1,b[1]-2},{b[0]-2,b[1]-1},{b[0]-2,b[1]+1},{b[0]-2,b[1]+2}};

Later after reading some StackOverflow posts, I realized I have to use possw[][] but that too gave me an error.后来在阅读了一些 StackOverflow 帖子后,我意识到我必须使用possw[][]但这也给了我一个错误。

error: expected primary-expression before ']' token

What should I do?我该怎么办?

You can't assign to an array, only copy to it.您不能分配给数组,只能复制到它。

One possible solution here is to make another array, say eg new_passw that you initialize :这里一个可能的解决方案是创建另一个数组,例如你初始化的new_passw

int new_passw[8][2] = {{b[0]+1,b[1]+2}, ... }};

Then you copy from this new_passw array into the old passw array.然后从这个new_passw数组复制到旧的passw数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM