How to solve C++ syntax error: 'expected ':' before ';' token'?
How to solve C syntax error: 'expected ':' before ';' token'
C is a powerful and flexible programming language, but sometimes we may You will encounter some syntax errors, such as "expected ':' before ';' token". This error message is usually caused by a syntax error and the compiler cannot recognize the correct syntax structure. In this article, we'll cover some common reasons why things go wrong and how to fix them.
- Reference type error
In C, reference types are usually represented by the & symbol. If we forget to add the ampersand when declaring a reference variable, the above error will occur. For example:
int main() { int a; int &b = a; // 错误:缺少引用符号& return 0; }
Solution: When quoting a variable declaration, make sure to add an & symbol before the reference symbol.
int main() { int a; int &b = a; // 正确:添加引用符号& return 0; }
- Function bracket error
In C, the parameter and return value types of a function are usually placed within brackets. If we omit the parentheses in the function declaration, the above error will occur. For example:
int main { // 错误:缺少函数括号 return 0; }
Solution: When declaring a function, make sure to add parentheses after the function name.
int main() { // 正确:添加函数括号 return 0; }
- Statement terminator error
In C, statements usually end with a semicolon. If we forget to add a semicolon at the end of the statement, the above error will occur. For example:
int main() { int a = 10 // 错误:缺少分号 return 0; }
Solution: At the end of the statement, make sure to add a semicolon.
int main() { int a = 10; // 正确:添加分号 return 0; }
- Type declaration error
In C, we must declare the type of a variable before using it. The above error will occur if we forget to declare the type before using the variable. For example:
int main() { a = 10; // 错误:缺少变量类型声明 return 0; }
Solution: Make sure to declare the variable's type before using it.
int main() { int a = 10; // 正确:添加变量类型声明 return 0; }
- Header file reference error
In C, we can use the #include directive to introduce header files. If we reference a header file that is not found in the program, the above error will occur. For example:
#include <iostream2> // 错误:找不到头文件 int main() { return 0; }
Solution: Make sure the referenced header file exists and spell the header file name correctly.
#include <iostream> // 正确:引用正确的头文件 int main() { return 0; }
In summary, the 'expected ':' before ';' token' error is usually caused by syntax errors. By examining aspects such as reference types, function brackets, statement terminators, type declarations, and header file references, we can determine the cause of the error and make appropriate repairs. Proficiency in C syntax rules and careful code checking can help avoid such syntax errors.
The above is the detailed content of How to solve C++ syntax error: 'expected ':' before ';' token'?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



How to solve C++ syntax error: 'expectedprimary-expressionbefore','token'? Overview: When writing C++ code, we sometimes encounter various errors. One of them is "expectedprimary-expressionbefore','token" (missing primary expression before comma). This error is usually detected during the compilation process, it prompts us that a

How to solve file permission issues in C++ development During the C++ development process, file permission issues are a common challenge. In many cases, we need to access and operate files with different permissions, such as reading, writing, executing and deleting files. This article will introduce some methods to solve file permission problems in C++ development. 1. Understand file permissions Before solving file permissions problems, we first need to understand the basic concepts of file permissions. File permissions refer to the file's owner, owning group, and other users' access rights to the file. In Li

How to solve C++ syntax error: 'expectedprimary-expressionbefore'.'token'? When writing programs in C++, we sometimes encounter various syntax errors. One of the common errors is 'expectedprimary-expressionbefore'.'token'. When we use incorrect syntax to access members of a class in our code, the compiler will report this error.

How to solve C++ syntax error: 'expectedinitializerbefore'('token'? In C++ programming, you often encounter various compilation errors. One of the common errors is 'expectedinitializerbefore'('token'. In this article, we The cause of this error will be discussed in detail and a solution will be provided. First, let's look at a simple example: #include&l

How to solve the multi-threaded communication problem in C++ development. Multi-threaded programming is a common programming method in modern software development. It allows the program to perform multiple tasks at the same time during execution, improving the concurrency and responsiveness of the program. However, multi-threaded programming will also bring some problems, one of the important problems is the communication between multi-threads. In C++ development, multi-threaded communication refers to the transmission and sharing of data or messages between different threads. Correct and efficient multi-thread communication is crucial to ensure program correctness and performance. This article

WordPress is a powerful open source content management system that is widely used in website construction and blog publishing. However, in the process of using WordPress, sometimes you encounter the problem of Chinese content displaying garbled characters, which brings troubles to user experience and SEO optimization. Starting from the root cause, this article introduces the possible reasons why WordPress Chinese content displays garbled characters, and provides specific code examples to solve this problem. 1. Cause analysis Database character set setting problem: WordPress uses a database to store the website

How to solve C++ syntax errors: 'expectedprimary-expressionbefore'*'token' In the process of learning C++ programming, we often encounter various syntax errors. One of the common errors is 'expectedprimary-expressionbefore'*'token'. This error usually occurs when using pointers, mainly because we use wrong syntax somewhere or forget

Summary of frequently asked questions about importing Excel data into Mysql: How to solve the problem of field type mismatch? Importing data is a very common operation in database management, and Excel, as a common data processing tool, is usually used for data collection and organization. However, when importing Excel data into a Mysql database, you may encounter field type mismatch problems. This article will discuss this issue and provide some solutions. First, let’s understand the origin of the problem of field type mismatch.
