C++函数中未定义Switch Case问题咨询——附滚动菜单代码
Hey there! Let's work through that undefined switch case issue you're hitting with your scrolling console menu. First, let's clean up the code you shared (note it looks truncated in your original snippet) and break down the likely problems.
Your Truncated Code (Formatted)
#include <vector> #include <string> // Missing required headers like <windows.h>, <iostream>, <tchar.h> here? using namespace std; vector<string> menuVector; void ShowConsoleCursor(bool showFlag) { // Assuming you have this function defined somewhere, else it's another undefined issue! HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE); CONSOLE_CURSOR_INFO cursorInfo; GetConsoleCursorInfo(out, &cursorInfo); cursorInfo.bVisible = showFlag; SetConsoleCursorInfo(out, &cursorInfo); } void welcome() { // Your welcome message function - make sure this is defined too! cout << "=== My Scrolling Menu ===" << endl; } void scrollingMenu_() { ShowConsoleCursor(false); int pointer = 0; while (true) { system("cls"); SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15); welcome(); for (int i = 0; i < menuVector.size(); ++i) { if (i == pointer) { // Truncated line in your code - fixed to set selected item color SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 14); // Yellow for selected cout << "> " << menuVector[i] << endl; SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15); // Reset to white } else { cout << " " << menuVector[i] << endl; } } // Missing keyboard input handling + switch case here! // This is likely where your "undefined switch case" error comes from if (GetAsyncKeyState(VK_UP) & 0x8000) { pointer = (pointer == 0) ? menuVector.size() - 1 : pointer - 1; Sleep(100); // Prevent rapid scrolling } else if (GetAsyncKeyState(VK_DOWN) & 0x8000) { pointer = (pointer == menuVector.size() - 1) ? 0 : pointer + 1; Sleep(100); } else if (GetAsyncKeyState(VK_RETURN) & 0x8000) { // Properly implemented switch case for menu selections switch(pointer) { case 0: cout << "Logging in..." << endl; Sleep(1000); break; case 1: cout << "Exiting program..." << endl; Sleep(1000); exit(0); default: break; } } } } int _tmain(int argc, _TCHAR* argv[]) { menuVector.push_back("Login"); menuVector.push_back("Exit"); scrollingMenu_(); cin.get(); return 0; }
Common Causes for "Undefined Switch Case" Errors
- Missing switch statement entirely: Your original
scrollingMenu_function was truncated right where you'd handle user input. It’s likely you either hadn’t implemented the switch case yet, or had a typo in referencing it, leading to the compiler error. - Undefined constants in case labels: If you used an enum or custom constant (like
case MENU_LOGIN:) without declaring it first, the compiler will throw an undefined error. - Syntax mistakes: Forgetting colons after case labels, mismatching the switch expression type (e.g., trying to use a string in a switch pre-C++17), or misspelling case values can trigger this issue.
- Missing required headers: Functions like
GetAsyncKeyStateorSetConsoleTextAttributedepend on<windows.h>. Skipping this header can lead to undefined function errors that you might mistake for switch case problems.
Fix Steps to Apply
- Add all required headers: Make sure you include
<windows.h>,<iostream>,<vector>,<string>, and<tchar.h>at the top of your file. - Implement the switch case properly: As shown in the code above, use the menu pointer index in the switch to handle each menu option clearly.
- Verify helper functions are defined:
ShowConsoleCursorandwelcomeneed full implementations (I added a sampleShowConsoleCursorsince it was missing in your snippet). - Check for typos: Ensure case labels match the values of your switch expression (in this case, the integer
pointervalues: 0 for Login, 1 for Exit).
If you’re still hitting errors, share the exact compiler error message and your full code—this will help narrow down the issue even faster!
内容的提问来源于stack exchange,提问作者JoJo
相关产品推荐
相关产品推荐

