C++:在 switch 语句中构造数组
C++: Construct arrays inside a switch statement
我正在尝试构造一个具有几种不同配置的数组,其中将随机选择一个。我首先尝试的代码是:
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
}
但是我得到了编译器错误:
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};
...
我接下来尝试:
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
}
但这又返回了一个编译器错误:
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};
...
我知道我可以在每个 case 语句中都有一个 for 循环,然后将值分配给在 switch 语句之外创建的 tileVals 数组,但这似乎不是一种非常有效的方法。 有没有更好的方法?谢谢。
如果不在
后面的代码中修改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
}
- 因此,您的第一个示例失败了,因为在 switch 语句中声明的所有变量都具有相同的作用域。所以你正在重新定义变量。
- 第二个示例失败,因为您正在访问超出范围的堆栈内存。
对此没有很好的解决方案...我可能会做:
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
}
如果你真的需要编辑tileVals
的内容,那么我可能会在switch语句之前为tileVals
分配内存,然后使用memcpy()
。
要在 case 语句中创建变量,必须将大小写括在大括号中。
switch(condition) {
case 0: {
int x = 0;
break;
}
}
第二个编译器错误可以用一个更简单的例子来解释:
void foo()
{
goto end:
int x = 0;
end:
}
当我编译仅包含该函数的文件时,出现以下错误。
g++ -c -o test-33.o test-33.cctest-33.cc:在函数 'void foo()' 中:test-33.cc:5:7:错误:跳转到标签"结束" [-允许]test-33.cc:3:12:错误:从这里 [-允许]test-33.cc:4:11:错误:交叉初始化"int x"test-33.cc:6:5:错误:"}"标记之前的预期主表达式test-33.cc:6:5:错误:在"}"标记之前应为";"
这对我来说意味着,我们正在执行一个 goto
语句,该语句跳转变量x
的初始化。您可以在end:
之后引用x
,即使绕过了x
的初始化。
您在开关块中看到类似的错误case ...:
因为语句是跳跃点,就像goto
语句的跳跃点一样。
相关文章:
- 比较if语句中的数组值和int值
- 为什么我的 if else 语句不起作用并从数组中输出正确的索引?
- 任意大小的 constexpr 数组是否可以用作 switch 语句中的案例?
- 有人可以解释一下吗?这是关于数组和数组内部 if 语句的一些处理
- 在编译时自动生成用于稀疏数组索引的switch语句
- C++ 将二维数组索引与条件语句中的函数值进行比较
- 如果带有字符数组的语句无法正常工作
- 如何在Char数组中搜索Char在IF条件语句中
- 使用 if (? 运算符) 语句传递带有指针的数组
- C 搜索数组以获取一个数字,但如果找不到该数组,则不会触发其他语句
- If/Else 语句在 2D 数组中移动时出现问题
- 显示使用函数、switch 语句和多维数组的程序的多个不合逻辑的错误
- 2D数组-将三个元素与if语句进行比较
- visual在C++中的if语句中的数组中使用特定值
- for 语句内的动态数组
- 如何使用嵌套的 if 语句检查字符数组
- 无法弄清楚为什么我的程序在条件不为真时执行 if 语句(数组)
- 正在尝试使用if语句检查数组数据
- 如何在不使用if语句的情况下查找数组中的最大值和最小值
- 将字符串存储在要在 IF 语句中使用的数组中的可能方法