小编Ale*_*lex的帖子

我可以在case语句中使用数组吗?

我想在C++的switch/case语句中使用const int数组.可能吗?到目前为止,我尝试过类似的东西:

int main()
{
    int const tab[3] = {1,2,3};
    int value(2);
    switch(value)
    {
        case tab[1]:
            cout << "result is: " << tab[0]<< endl;
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

然而,编译器一直告诉我:

.../main.cpp|11|error: the value of ‘tab’ is not usable in a constant expression
Run Code Online (Sandbox Code Playgroud)

我把我的数组声明为"int const",这还不够吗?

c++ arrays case switch-statement

3
推荐指数
1
解决办法
4541
查看次数

标签 统计

arrays ×1

c++ ×1

case ×1

switch-statement ×1