[英]C++: Construct arrays inside a switch statement
I'm trying to construct an array that has a few different configurations where one will be selected randomly. 我正在尝试构建具有一些不同配置的数组,其中将随机选择一个。 The code I tried first is:
我首先尝试的代码是:
void createArray() {
int* tileVals;
int randInt = rand() % 3;
switch (randInt) {
case 0:
int tileVals[] = {1,1,1,2,2,2,3,3,3};
break;
case 1:
int tileVals[] = {1,1,1,1,2,2,3,3,3};
break;
case 2:
int tileVals[] = {1,1,1,1,2,2,2,3,3};
break;
}
// Do stuff with tileVals
}
But I got the compiler error: 但是我得到了编译器错误:
src/Board.cpp:67:7: error: redefinition of 'tileVals'
int tileVals[] = {1,1,1,1,2,2,2,3,3};
src/Board.cpp:70:7: error: redefinition of 'tileVals'
int tileVals[] = {1,1,1,2,2,2,2,3,3};
...
I next tried: 接下来,我尝试:
void createArray() {
int* tileVals;
int randInt = rand() % 3;
switch (randInt) {
case 0:
int case0[] = {1,1,1,2,2,2,3,3,3};
tileVals = case0;
break;
case 1:
int case1[] = {1,1,1,1,2,2,3,3,3};
tileVals = case1;
break;
case 2:
int case2[] = {1,1,1,1,2,2,2,3,3};
tileVals = case2;
break;
}
// Do stuff with tileVals
}
But this came back with a compiler error: 但这又返回了编译器错误:
src/Board.cpp:85:2: error: switch case is in protected scope
case 6:
^
src/Board.cpp:82:7: note: jump bypasses variable initialization
int case5[] = {1,1,1,2,2,3,3,3,3};
...
I know I could do it by having a for loop inside each case statement that then assigned the values to a tileVals array that is created outside the switch statement, but that doesn't seem like a very efficient way to do this. 我知道我可以通过在每个case语句中包含一个for循环,然后将值分配给在switch语句外部创建的tileVals数组来做到这一点,但这似乎不是一种非常有效的方法。 Is there a better way?
有没有更好的办法? Thanks.
谢谢。
If you do not modify the content of tileVals
in the later code, you could do it as follows: 如果不在以后的代码中修改
tileVals
的内容,则可以按照以下步骤进行操作:
void createArray() {
int * tileVals;
int randInt = rand() % 3;
int cases[][9] = { {1, 1, 1, 2, 2, 2, 3, 3, 3},
{1, 1, 1, 1, 2, 2, 3, 3, 3},
{1, 1, 1, 1, 2, 2, 2, 3, 3} };
tileVals = cases[randInt];
// Do stuff with tileVals
}
There's not a great solution to this... I might do: 没有一个很好的解决方案...我可能会这样做:
void createArray() {
static const int case0[] = {1,1,1,2,2,2,3,3,3};
static const int case1[] = {1,1,1,1,2,2,3,3,3};
static const int case2[] = {1,1,1,1,2,2,2,3,3};
const int *tileVals = NULL;
int randInt = rand() % 3;
switch (randInt) {
case 0:
tileVals = case0;
break;
case 1:
tileVals = case1;
break;
case 2:
tileVals = case2;
break;
}
// Do stuff with tileVals
}
If you actually needed to edit the contents of tileVals
, then I would probably allocate the memory for tileVals
before the switch statement, and then use a memcpy()
. 如果您实际上需要编辑
tileVals
的内容,那么我可能会在switch语句之前为tileVals
分配内存,然后使用memcpy()
。
To create variables in case statements, you must enclose the case in braces. 要在case语句中创建变量,必须将case括在大括号中。
switch(condition) {
case 0: {
int x = 0;
break;
}
}
The second compiler error can be explained with a simpler example: 第二个编译器错误可以用一个更简单的示例来解释:
void foo()
{
goto end:
int x = 0;
end:
}
When I compile a file containing just that function, I get the following error. 当我编译仅包含该功能的文件时,出现以下错误。
g++ -c -o test-33.o test-33.cc test-33.cc: In function ‘void foo()’: test-33.cc:5:7: error: jump to label ‘end’ [-fpermissive] test-33.cc:3:12: error: from here [-fpermissive] test-33.cc:4:11: error: crosses initialization of ‘int x’ test-33.cc:6:5: error: expected primary-expression before ‘}’ token test-33.cc:6:5: error: expected ‘;’ before ‘}’ token
What this means to me, is that we are executing a goto
statement that jumps initialization of variable x
. 对我而言,这意味着我们正在执行
goto
语句,该语句跳过变量x
初始化。 You could reference x
after end:
even though initialization of x
was by-passed. 您可以在
end:
后引用x
end:
即使绕过x
初始化。
You are seeing similar errors in the switch block since case ...:
statements are jumping points like the jumping points of goto
statements. 您会在switch块中看到类似的错误,因为
case ...:
语句就像goto
语句的跳转点一样是跳转点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.