Home > Backend Development > C++ > body text

C++ syntax error: The default value of a member must be provided at declaration time. How to deal with it?

王林
Release: 2023-08-22 09:06:28
Original
1365 people have browsed it

C is a high-level programming language widely used in system development, game development, algorithm competition and other fields. However, due to its complex syntax and error-prone characteristics, various errors often occur in C programming. One of the more common errors is the error "the default value of a member must be provided at the time of declaration".

The reason for this error is that C stipulates that member variables must give default values ​​in the definition of the class. Sometimes during the code writing process, you forget to specify a default value for a member variable, or assign a value to a member variable in the constructor, but do not provide a default value in the class definition.

Below we introduce several common processing methods:

  1. Give the default value

The most direct method is to give it in the definition of the class The default value of the member variable is as follows:

class MyClass
{
public:
    int myInt = 0; // 成员变量的默认值在声明时提供
};
Copy after login
  1. Initialized in the constructor

If the default value cannot be given in the definition of the class, it can be given in the class definition Initialize member variables in the constructor as follows:

class MyClass
{
public:
    MyClass() : myInt(0) {} // 在构造函数中初始化成员变量
private:
    int myInt;
};
Copy after login

This ensures that the member variables have a clear default value and avoids compilation errors.

  1. Delete the default constructor

If the member variables of the class cannot be initialized by default, you can avoid the default value problem of the member variables by deleting the default constructor. For example:

class MyClass
{
public:
    MyClass(int x) : myInt(x) {} // 非默认构造函数
private:
    int myInt;
};
Copy after login

By deleting the default constructor, the compiler cannot automatically generate a default constructor, and there will be no problem with the default value of member variables.

The above are several common ways to deal with the error "The default value of a member must be provided at the time of declaration". It should be noted that in order to avoid this kind of error, it is best to specify a default value for all member variables when defining a class.

The above is the detailed content of C++ syntax error: The default value of a member must be provided at declaration time. How to deal with it?. 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