简体   繁体   English

C ++:在switch语句内构造数组

[英]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
}
  1. So your first example fails because all of the variables declared within a switch statement have the same scope. 因此,第一个示例失败,因为在switch语句中声明的所有变量都具有相同的作用域。 So you're redefining variables. 因此,您正在重新定义变量。
  2. Your second example fails because you are accessing a piece of stack memory that has gone out of scope. 第二个示例失败,因为您正在访问超出范围的堆栈内存。

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.

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