Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

error: expected primary-expression before X token

Tags:

c++

Can you explain what the title error usually means?

I get it sometimes and I always eventually manage to fix it by accident, but I still don't have a clue what it means.

Here's an example from my current error:

Lca<RankVec, VertexVec> lca(graphList[0], dset, ss&);

error: expected primary-expression before ')' token

like image 735
Amir Rachum Avatar asked Nov 11 '10 11:11

Amir Rachum


People also ask

What does Error expected primary expression before token mean?

This typically means that your code is missing a closing '}'. For every opening '{' there should be a closing '}' expected primary-expression before ')' token. Sometimes this happens when the variable passed into a function isn't the type the function expected.

What is expected primary expression before token Arduino?

A “define” replaces the text before the compiler starts compiling. A “const int” is a fixed number and is a little more preferred for pin assignments.

What is a primary expression in C?

Primary expressions are the building blocks of more complex expressions. They may be constants, identifiers, a Generic selection, or an expression in parentheses.


1 Answers

Hard to tell without any sample, but IIRC this happens as a consequence of using an undefined symbol (e.g. a function or a type without a declaration - not sure which exactly). In consequence, the parser gets confused and doesn't know what to expect further on in the code.

-(I believe this error only appears in conjunction with other errors? Or can you provide a code snippet which would give only this error on GCC compiler?)-

edit: In the code you provided, it is just the parser getting lost after encountering a "&" symbol in an illegal place - so a consequence of invalid syntax, not invalid semantics.

Such cryptic error messages (or often worse) are the consequence of the fact that C++'s grammar is undecidable and the compiler, upon seeing an error, cannot really guess what was supposed to be there and thus cannot generate a more accurate description.

like image 80
Kos Avatar answered Sep 25 '22 02:09

Kos