以下代碼使用帶有枚舉的開關。主程序正確地將參數傳遞給該函數,但不執行正確的開關行。你能建議它爲什麼不進入開關條件嗎?如何在開關狀態下使用枚舉
enum MyEnum {
Enum1 = 1,
Enum2 = 0x0D
};
bool compute(MyEnum code) {
switch(code) {
Enum1: return true;
Enum2: return false;
};
cout << "why here??" << endl; // this line is getting printed for both inputs
return false;
}
int main() {
cout << "compack=" << compute((MyEnum)1) << endl; // printed "0"
cout << "compack=" << compute((MyEnum)13) << endl; // printed "0"
}
我檢查(如3019153)切換和枚舉相關的其他問題,但不能找出錯誤。
哇,編譯器甚至沒有抱怨?非常感謝。 – R71
@ R71它可能不會抱怨,因爲兩個因素: 第一: 'FOO:'簡直就是一個標籤(可以使用'轉到富;'跳轉到這個標籤,例如。) 二: 您可能沒有使用-Wall標誌(它顯示了一些有用的警告): ' ▶g ++ -Wall switch.cpp -o 1 switch.cpp:在函數'int main()'中: switch.cpp:7: 9:警告:標籤'foo'已定義但未使用[-Wunused-label] foo:return 1; ^ ' –