Home > Backend Development > C++ > body text

How to solve C++ syntax error: 'expected primary-expression before ';' token'?

WBOY
Release: 2023-08-25 21:34:42
Original
19901 people have browsed it

如何解决C++语法错误:\'expected primary-expression before \';\' token\'?

How to solve C syntax error: 'expected primary-expression before ';' token'

When learning and writing C code, we often encounter various syntaxes mistake. One of the common errors is 'expected primary-expression before ';' token'. This error often occurs when a semicolon is used to end a statement, but the structure of the statement is incorrect. This article details the cause of this error and provides a solution.

There are usually two reasons for this error: the variable is not defined correctly or the wrong syntax structure is used. Below we will explain these two situations respectively and provide corresponding solutions.

Situation 1: Incorrectly defined variables

When we reference an incorrectly defined variable in C code, the compiler will report an error 'expected primary-expression before ';' token '. This usually happens in the following situations:

  1. Improper declaration of variable type: In C, we need to declare the type of the variable before using it. If we forget to define the type of a variable before using it, it will result in a compilation error. For example:
x = 5;
Copy after login

In the above code, the type of variable x is not defined, and the compiler will report an error.

Solution: Declare the type of the variable correctly. For example:

int x = 5;
Copy after login
  1. Variable name spelling error: If we use an undeclared variable name (misspelling) in the code, the compiler will not recognize the variable.

Solution: Check whether the variable names in the code are spelled correctly and correct the errors.

int num = 10;
cout << nums << endl; // 正确的变量名是num,不是nums
Copy after login
  1. Variable scope problem: When the variable we reference exceeds its scope, the compiler will report an error. For example:
void func() {
    int x = 5;
}

int main() {
    cout << x << endl; // x在函数func的作用域内,无法在main函数中引用
    return 0;
}
Copy after login

Solution: Place the variable declaration in the appropriate scope.

Case 2: Using the wrong grammatical structure

When we use the wrong grammatical structure in the C code, the compiler will report the error 'expected primary-expression before ';' token '. This usually occurs in the following situations:

  1. Wrong expression: When we use wrong expressions in if, for, while and other statements, the compiler will report an error.

Solution: Check whether the expressions in the code are correct and correct the errors.

  1. Incorrect member access: When we use the dot operator to access an undefined object or use the accessor '->' to access a non-pointer variable, the compiler will report an error.

Solution: Check whether the member access operation in the code is correct and correct the error.

Here is a simple code example that demonstrates how to solve the C syntax error 'expected primary-expression before ';' token':

#include <iostream>
using namespace std;

int main() {
    int num = 10;
    cout << num << endl;

    return 0;
}
Copy after login

In the above code, we have declared the variables correctly The type of num and initialized before using the variable. Therefore, the compiler will not issue an error.

Summary:

When writing C code, we often encounter various syntax errors. When the 'expected primary-expression before ';' token' error occurs, we need to carefully check the code to find out and solve the cause of the error. This article provides two common error conditions and corresponding solutions, hoping to help readers solve this error. At the same time, it is recommended to maintain good coding style and specifications when coding, which can effectively reduce the occurrence of grammatical errors.

The above is the detailed content of How to solve C++ syntax error: 'expected primary-expression before ';' token'?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!