Amir Rachum
Amir Rachum

Reputation: 79735

error: expected primary-expression before X token

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

Upvotes: 5

Views: 4498

Answers (4)

MSalters
MSalters

Reputation: 180295

The & symbol is used as a bitwise AND operator. I.e. 0x3 & 0x5 == 0x1. In this form, it's an infix operator between two expressions. From what you've told us, we now know that ss is an expression. Therefore, the compiler thinks ss & is the start of a bitwise AND expression, and it expects the right-hand side (which it calls "primary-expression"). However, instead of a right-hand side, the compiler encounters a ). That's obviously not the right-hand side of an AND expression.

Upvotes: 2

liaK
liaK

Reputation: 11648

From Standard docs., 5.1 Primary expressions,

Primary expressions are literals, names, and names qualified by the scope resolution operator ::.

And also from 2.6 Tokens,

There are five kinds of tokens: identifiers, keywords, literals,16) operators, and other separators

Now, I believe the error is pretty descriptive..

Hope that helps..

Upvotes: 2

Chubsdad
Chubsdad

Reputation: 25537

Tough to suggest with the information in OP at the moment, but let me give it a try.

Assuming, that everying else is in place, for the expression in OP to be a function call, try changing the way you pass the last argument. If you want to pass it as a pointer, you have to use & before the name and not after.

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

Upvotes: 0

Kos
Kos

Reputation: 72319

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.

Upvotes: 3

Related Questions