syntax error : missing 'token1' before 'token2'
A syntax error is an error in the use of the language. A token is a language element such as a keyword or operator. Roughly translated, anything that is not whitespace (spaces, tabs, line feeds, comments, and so on) is a token.
The compiler expects certain language elements to appear before or after other elements. For instance, an if statement requires that the first non-whitespace character following the word if must be an opening parenthesis. If anything else is used, the compiler cannot “understand” the statement. For example:
if j < 25 // syntax error : missing '(' before 'j'
This error can also be caused by a missing closing brace ‘}’, right parenthesis ‘)’, or semicolon ‘;’. The missing token may belong on the line above where the error was actually detected and reported.
This error can also be caused by an invalid tag in a class declaration. The following are examples of these errors:
class X
{
int member
} x; // error, missing ; on previous line
class + {}; // error, + is an invalid tag name
This error can also be caused by placing a label by itself without being attached to a statement. If you need to place a label by itself, for example at the end of a compound statement, it can be attached to a null statement.
The following is an example of this error:
void func1()
{
end: // error
} // this line is not a statement
void func2()
{
end: // OK
; // this label attached to null statement
}
This error can also be caused when the ANSI compatibility option (/Za) is in effect and the source code contains single line comments.
The following is an example of this error:
int i; // syntax error : missing ';' before 'division operator'
int j; /* no error */
In the first case, the first single line comment character, ‘/’, is interpreted as a division operator since single line comments are not valid according to the ANSI C language standard. However, single line comments are legal in the C++ language.
Tips