title=變數不能出現在常量表達式中
外觀
雖然 switch 語句經常使用變數作為引數,但它們的 case 語句不能使用變數作為引數。
int foo = 7;
int bar = 4;
switch(foo) {
case bar:
cout << "This is case 0" << endl;
}
解決方案: 將 case 語句的引數設為常量
int foo = 7;
const int bar = 4;
switch(foo) {
case bar:
cout << "This is case 0" << endl;
}
- 在 GCC 版本 4.5.1 中發現的錯誤訊息
- 在 GCC 版本 3.2.3 中,報告為:case 標籤不能簡化為整數常量
- 此錯誤訊息可能與字串有關——但是,您無法建立字串常量