C++新手求助:编译报错expected primary-expression before ')' token
Hey there! As someone who’s struggled through those head-scratching C++ compiler error messages early on, let’s unpack what your errors mean and how to fix them.
First, let’s break down the error you’re seeing:
14:15: error: expected primary-expression before ')' token
18:20: error: expected primary-expression before ')' token
This error basically means the compiler can’t find a valid expression right before that closing parenthesis ). It’s one of the most common syntax hiccups for new C++ developers, and it usually stems from a few key issues:
- Missing function arguments: If you’re calling a function that requires parameters but forgot to pass them, the compiler gets confused. For example, if you have a function
add(int a, int b)and you writeadd();, this error will pop up. - Incomplete conditional expressions: In
if,while, or other control structures, if you leave one side of a comparison empty (likeif (x > )), the compiler doesn’t know what to comparexagainst. - Typos or undefined identifiers: If you misspell a variable/function name, or reference something the compiler hasn’t seen yet, it might interpret that as a missing expression before a parenthesis.
- Macro or template syntax mistakes: Sometimes macros can mess with parentheses, or template arguments might be missing or misformatted.
To give you a concrete example, here’s some code that would trigger exactly this error:
#include <iostream> void greet(std::string name); int main() { greet(); // Line with error: no argument passed to greet() if (5 > ) { // Another error: missing right-hand side of comparison std::cout << "Number is big!" << std::endl; } return 0; } void greet(std::string name) { std::cout << "Hello, " << name << "!" << std::endl; }
Since you mentioned lines 14 and 18 are the problem spots, could you share the code around those lines (or a minimal, complete example that reproduces the error)? That way I can pinpoint exactly what’s causing the issue and walk you through fixing it step by step.
内容的提问来源于stack exchange,提问作者andre2539

