C++函数编写报错求助:expected unqualified-id before ‘{’ token
Hey there! Let's walk through fixing your error and getting your admission function working as intended.
First: What Does That Error Mean?
The expected unqualified-id before ‘{’ token error pops up when the compiler expects a valid identifier (like a function name, variable name, or type) but encounters something unexpected. In your case, the root cause is simple:
Look at your function line:
void admission(double gpa, int score);
That trailing semicolon tells the compiler "this is the end of the function declaration". So when it hits the { on the next line, it has no idea what function this code block belongs to—hence the error, because it was expecting a function name (or other identifier) before that opening brace.
Other Issues in Your Code
Beyond that initial error, there are a few more things that will break your logic once you fix the first problem:
- Trailing semicolons on every
if/else if: Eachif (condition);ends the conditional statement immediately. The code blocks in{}that follow will run no matter what the condition evaluates to, and yourelseclauses will throw errors because they won't be attached to any validif. - Missing required headers: You're using
cout(from<iostream>) andexit()/EXIT_FAILURE(from<cstdlib>) but haven't included these headers. - Unqualified standard library calls:
cout,exit, andEXIT_FAILUREare part of thestdnamespace, so you need to prefix them withstd::or add ausing namespace std;line (the former is generally better practice to avoid namespace conflicts).
Corrected Code
Here's your code with all fixes applied:
#include <iostream> #include <cstdlib> void admission(double gpa, int score) { if (gpa < 0.0 || gpa > 4.0) { std::cout << "GPA must be between 0.0 and 4.0.\n"; std::exit(EXIT_FAILURE); } else if (score < 0 || score > 100) { std::cout << "Test Score must be between 0 and 100.\n"; std::exit(EXIT_FAILURE); } else if (gpa >= 3.6 && score >= 60) { std::cout << "The admission result is: Accepted!\n"; } else if (gpa >= 3.0 && score >= 70) { std::cout << "The admission result is: Accepted!\n"; } else if (gpa >= 2.6 && score >= 80) { std::cout << "The admission result is: Accepted!\n"; } else if (gpa >= 2.0 && score >= 90) { std::cout << "The admission result is: Accepted!\n"; } else { std::cout << "The admission result is: Rejected.\n"; } }
Quick Notes on the Fixes:
- Removed the trailing semicolon from the function definition line so the
{correctly starts the function body. - Removed all semicolons after
if/else ifconditions, so the code blocks only run when the condition is true. - Added
<iostream>and<cstdlib>headers so the compiler recognizescoutandexit(). - Prefixed standard library calls with
std::to resolve namespace issues. - Added newline characters (
\n) to make output cleaner.
A Quick Suggestion
You mentioned wanting the function to "get user input"—right now, this function takes gpa and score as parameters. If you want it to read input directly from the user, you could modify it to remove the parameters and use std::cin inside the function instead, like this:
#include <iostream> #include <cstdlib> void admission() { double gpa; int score; std::cout << "Enter your GPA: "; std::cin >> gpa; std::cout << "Enter your test score: "; std::cin >> score; // Rest of the validation and logic goes here... }
内容的提问来源于stack exchange,提问作者Alex Zimmerman

