Home  >  Article  >  Backend Development  >  C++ syntax error: Class members must be complete types, how to deal with it?

C++ syntax error: Class members must be complete types, how to deal with it?

WBOY
WBOYOriginal
2023-08-22 12:09:291103browse

C++ syntax error: Class members must be complete types, how to deal with it?

In C programming, programs often fail to compile correctly due to some simple errors. One of the more common mistakes is that class members must be complete types. This error message indicates that when defining a class member variable or member function, the data type used is not a complete type, causing the compiler to be unable to recognize the data type of the class member.

This type of error may occur in the case of cross-references between classes. For example, class A needs to reference class B, but class B has not been defined before class A is declared. At this time, this error may occur when the member variables or member functions defined in class A involve class B.

So how should this error be handled? Below I will provide two possible solutions:

Method 1: Pre-declared class

This method is a relatively simple and effective solution. When defining class A, class B can be declared in advance outside the class. In this way, when class B is used in class A, the compiler can recognize this type. The code implementation is as follows:

class B; //提前声明类B

class A {
    private:
        B* b_ptr;
    public:
        void foo();
};

class B {
    private:
        int b_var;
    public:
        void bar();
};

void A::foo() {
    b_ptr = new B;
}

void B::bar() {
    b_var = 10;
}

int main() {
    A a_obj;
    a_obj.foo();
    return 0;
}

In the above code, class B is first declared in advance before class A. In this way, class B can be used normally when defining class A. In class A, a pointer b_ptr pointing to class B is defined, and the pointer is assigned a value through the member function foo(). At the same time, class B also defines its own member variables and member functions normally, and assigns values ​​to the member variable b_var through the member function bar(). Finally, an instance a_obj of class A is created in the main function and the member function foo() is called. The program can be compiled and run correctly, and the error message "Class members must be complete types" no longer appears.

Method 2: Use pointers or references instead of objects

If you need to access member variables or member functions in another class in a class, but cannot put the class definition at the beginning, then this This method can be used. In this case, you can pass a pointer or reference to the object instead of the object itself. This also avoids "class members must be complete types" errors. The code is implemented as follows:

class B; //提前声明类B

class A {
    private:
        B* b_ptr;
    public:
        void foo();
};

class B {
    private:
        int b_var;
    public:
        void bar();
};

void A::foo() {
    b_ptr = new B;
    b_ptr -> bar();
}

void B::bar() {
    b_var = 10;
}

int main() {
    A a_obj;
    a_obj.foo();
    return 0;
}

Here, the definitions of class A and class B still have not changed, but in the foo function, we call the member function in class B by operating on the pointer b_ptr of class B. bar(). At the same time, in class B, the same scheme is used to access the member variable b_var.

The two methods are different, and the specific plan can be chosen according to your needs and circumstances. But in general, these solutions can avoid compilation errors caused by "class members must be complete types". Hope this article is helpful to you.

The above is the detailed content of C++ syntax error: Class members must be complete types, how to deal with 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