我有这个C ++程序:
int box[9][9];
box = { {solution[0][0],solution[0][1],solution[0][2],solution[1][0],solution[1][1],solution[1][2],solution[2][0],solution[2][1],solution[2][2]},
{solution[0][3],solution[0][4],solution[0][5],solution[1][3],solution[1][4],solution[1][5],solution[2][3],solution[2][4],solution[2][5]},
{solution[0][6],solution[0][7],solution[0][8],solution[1][6],solution[1][7],solution[1][8],solution[2][6],solution[2][7],solution[2][8]},
{solution[3][0],solution[3][1],solution[3][2],solution[4][0],solution[4][1],solution[4][2],solution[5][0],solution[5][1],solution[5][2]},
{solution[3][3],solution[3][4],solution[3][5],solution[4][3],solution[4][4],solution[4][5],solution[5][3],solution[5][4],solution[5][5]},
{solution[3][6],solution[3][7],solution[3][8],solution[4][6],solution[4][7],solution[4][8],solution[5][6],solution[5][7],solution[5][8]},
{solution[6][0],solution[6][1],solution[6][2],solution[7][0],solution[7][1],solution[7][2],solution[8][0],solution[8][1],solution[8][2]},
{solution[6][3],solution[6][4],solution[6][5],solution[7][3],solution[7][4],solution[7][5],solution[8][3],solution[8][4],solution[8][5]},
{solution[6][6],solution[6][7],solution[6][8],solution[7][6],solution[7][7],solution[7][8],solution[8][6],solution[8][7],solution[8][8]}};
我得到的错误是:
error: assigning to an array from an initializer list
这是什么意思?
答案 0 :(得分:4)
声明变量时必须使用初始值设定项:
int box[9][9]= { {solution[0][0],solution[0][1],solution[0][2],solution[1][0],solution[1][1],solution[1][2],solution[2][0],solution[2][1],solution[2][2]},
{solution[0][3],solution[0][4],solution[0][5],solution[1][3],solution[1][4],solution[1][5],solution[2][3],solution[2][4],solution[2][5]},
{solution[0][6],solution[0][7],solution[0][8],solution[1][6],solution[1][7],solution[1][8],solution[2][6],solution[2][7],solution[2][8]},
{solution[3][0],solution[3][1],solution[3][2],solution[4][0],solution[4][1],solution[4][2],solution[5][0],solution[5][1],solution[5][2]},
{solution[3][3],solution[3][4],solution[3][5],solution[4][3],solution[4][4],solution[4][5],solution[5][3],solution[5][4],solution[5][5]},
{solution[3][6],solution[3][7],solution[3][8],solution[4][6],solution[4][7],solution[4][8],solution[5][6],solution[5][7],solution[5][8]},
{solution[6][0],solution[6][1],solution[6][2],solution[7][0],solution[7][1],solution[7][2],solution[8][0],solution[8][1],solution[8][2]},
{solution[6][3],solution[6][4],solution[6][5],solution[7][3],solution[7][4],solution[7][5],solution[8][3],solution[8][4],solution[8][5]},
{solution[6][6],solution[6][7],solution[6][8],solution[7][6],solution[7][7],solution[7][8],solution[8][6],solution[8][7],solution[8][8]}};
答案 1 :(得分:1)
此C ++错误:
error: assigning to an array from an initializer list
是因为您尝试使用不允许的初始化程序分配数组而导致的。创建数组时会发生初始化,并在创建数组时将值推入其中。当数组已经创建并且在其中放置新值时,就会发生赋值。初始化或分配期间允许执行的操作之间存在差异。
以下是导致上述错误的最简单代码:
#include <cstdlib>
#include <iostream>
using namespace std;
int main(int argc, char** argv) {
int myarray [] = {1,2,4};
myarray [] = {2,5,9};
//error, cannot assign with an initializer.
//You can't initialize an array that's already been initialized.
return 0;
}
如果您想在其中强制使用新值,请尝试std::copy
:
int myarray [] = {1,2,3,4};
int newarray [] = {2,5,9,12};
//copy the first 4 elements from newarray into newarray
std::copy(newarray, newarray + 4, myarray);
for(int x = 0; x < 4; x++){
cout << myarray[x] << ",";
}
打印:2,5,9,12,
但是你应该问自己为什么你这样编程接近金属。
考虑使用std :: vector代替:
#include <iostream>
#include <vector>
using namespace std;
int main(int argc, char** argv) {
vector<int> array;
cout << "size is: " << array.size() << endl;
array.push_back(42);
cout << "size is: " << array.size() << endl;
cout << "data items: " << array.at(0) << endl;
array.insert(array.begin(), 5);
array.insert(array.end(), 20);
cout << "size is: " << array.size() << endl;
int len = array.size();
for(int x = 0; x < array.size(); x++){
cout << "data item: " << array.at(x) << endl;
}
return 0;
}
打印出来:
size is: 0
size is: 1
data items: 42
size is: 3
data item: 5
data item: 42
data item: 20
使用C ++向量重新初始化列表:
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int foobar[3] = {1, 2, 3};
std::vector<int> myvector(foobar, foobar + 3);
int moobar[3] = {4, 5, 6};
myvector.assign(moobar, moobar + 3);
for(int x = 0; x < 3; x++)
cout << myvector[x];
}