Home > Backend Development > C++ > How to solve C++ syntax error: 'expected ':' before ';' token'?

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

PHPz
Release: 2023-08-25 15:42:43
Original
7966 people have browsed it

如何解决C++语法错误:\'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.

  1. 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;
}
Copy after login

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;
}
Copy after login
  1. 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;
}
Copy after login

Solution: When declaring a function, make sure to add parentheses after the function name.

int main() {
  // 正确:添加函数括号
  return 0;
}
Copy after login
  1. 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;
}
Copy after login

Solution: At the end of the statement, make sure to add a semicolon.

int main() {
  int a = 10; // 正确:添加分号
  return 0;
}
Copy after login
  1. 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;
}
Copy after login

Solution: Make sure to declare the variable's type before using it.

int main() {
  int a = 10; // 正确:添加变量类型声明
  return 0;
}
Copy after login
  1. 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;
}
Copy after login

Solution: Make sure the referenced header file exists and spell the header file name correctly.

#include <iostream> // 正确:引用正确的头文件
int main() {
  return 0;
}
Copy after login

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!

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