【发布时间】:2015-05-30 18:26:20
【问题描述】:
我想在 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;
}
但是编译器一直告诉我:
.../main.cpp|11|error: the value of ‘tab’ is not usable in a constant expression
好吧,我将我的数组声明为“int const”,这还不够吗?
【问题讨论】:
标签: c++ arrays switch-statement case