Home  >  Article  >  Backend Development  >  C++ syntax error: Members defined later must be placed after previously defined members. How should I correct it?

C++ syntax error: Members defined later must be placed after previously defined members. How should I correct it?

WBOY
WBOYOriginal
2023-08-22 08:33:47687browse

C is a high-level programming language that is widely used in various fields. However, when we write C code, we often encounter some syntax errors. One of the common errors is: "Members defined later must be placed after members defined previously", which can cause some problems at compile time.

Error message: Members defined later must be placed after previously defined members

Error analysis: This syntax error usually means that we change the order of member definitions when defining a class. wrong. In C, members of a class must be initialized in the order defined, otherwise this error will occur.

Solution: To solve this error, we need to rearrange the order of definition of the members in our class. The specific steps are as follows:

1. Find the definition statement where the error occurred. It can be found in the compiler error message.

2. Find all member definitions before this member definition.

3. Move this member definition to the appropriate location so that it is after all previously defined member definitions.

4. Save the file and recompile the code.

The following is a sample code:

class SomeClass {
public:
    int a;
    int c;
    int b; // 这里的b定义在了c的后面,会引发语法错误

    SomeClass(int a, int b, int c) {
        this->a = a;
        this->b = b;
        this->c = c;
    }
};

In the above sample code, we define a class named SomeClass. In this class, we need to define the members of the class a, c and b in order. But in the code, we defined b after c, which resulted in the above syntax error.

To solve this error, we need to make the following modifications:

class SomeClass {
public:
    int a;
    int b; // 将b定义到了c之前
    int c;

    SomeClass(int a, int b, int c) {
        this->a = a;
        this->b = b;
        this->c = c;
    }
};

In the above code, we moved the definition position of member b to c Previously, this code would compile normally.

Summary: In C, we need to initialize members in the order they are defined. If the error "Members defined later must be placed after previously defined members" occurs, we need to rearrange the definition order of the members in our class to comply with C's syntax rules and compile our code smoothly.

The above is the detailed content of C++ syntax error: Members defined later must be placed after previously defined members. How should I correct it?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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