Home  >  Article  >  Backend Development  >  Solve the problem of 'error: incomplete type is not allowed' in C++ code

Solve the problem of 'error: incomplete type is not allowed' in C++ code

王林
王林Original
2023-08-26 20:54:351878browse

解决C++代码中出现的“error: incomplete type is not allowed”问题

Solve the "error: incomplete type is not allowed" problem in C code

In the process of C programming, sometimes you will encounter some compilation errors. One of the common errors is "error: incomplete type is not allowed". This error is usually caused by operating on an incomplete type. This article will explain the cause of this error and provide several solutions.

First, let’s look at a sample code that causes this error:

#include <iostream>

class A;
class B {
public:
    void foo(A& a) {
        std::cout << "foo function" << std::endl;
    }
};

class A {
public:
    void bar(B& b) {
        b.foo(*this);
    }
};

int main() {
    A a;
    B b;
    a.bar(b);
    return 0;
}

In this sample code, we define two classes A and B. There is a member function bar in class A , the parameter type of this function is a reference to B, and there is a member function foo in class B, and the parameter type of this function is a reference to A. In the main function, we create a class A object a and a class B object b, and then call the bar function of a, passing b as a parameter.

However, when we try to compile this code, we will get the following error message:

error: incomplete type 'A' used in nested name specifier

This error is due to the compiler being unable to determine the complete definition of class A when compiling this code. caused. Since the existence of class A is only declared when defining class B, but the complete definition of class A is not provided, the compiler cannot determine the specific implementation of member functions and member variables in class A, resulting in compilation errors.

To solve this problem, we have several methods to try.

The first method is to place the definition of the class before the place where this class is used. We can swap the definition of class B with the definition of class A, as follows:

class A {
public:
    void bar(B& b);
};

class B {
public:
    void foo(A& a) {
        std::cout << "foo function" << std::endl;
    }
};

void A::bar(B& b) {
    b.foo(*this);
}

By placing the definition of class B before the definition of class A, the compiler can find the complete definition to resolve compilation errors.

Another approach is to use forward declarations. We can use the keyword "class" before the declaration of the class to make a forward declaration, as shown below:

class A;
class B {
public:
    void foo(A& a) {
        std::cout << "foo function" << std::endl;
    }
};

class A {
public:
    void bar(B& b);
};

void A::bar(B& b) {
    b.foo(*this);
}

By using the forward declaration, we tell the compiler that a class named A exists, but the The specific definition of the class is provided later in the code. In this way, the compiler can obtain the information of class A through forward declaration, thereby solving compilation errors.

The last method is to put the definition of the class in a header file and include the header file in the files that need to use this class. For example, we can put the definitions of class A and class B in two header files "aclass.h" and "bclass.h" respectively, and then include the corresponding header files in the files that use these two classes, as shown below :

In the "aclass.h" file:

#ifndef ACLASS_H
#define ACLASS_H

class B;

class A {
public:
    void bar(B& b);
};

#endif

In the "bclass.h" file:

#ifndef BCLASS_H
#define BCLASS_H

#include <iostream>
#include "aclass.h"

class B {
public:
    void foo(A& a) {
        std::cout << "foo function" << std::endl;
    }
};

#endif

In the files that use these two classes, use The #include directive includes the corresponding header file and uses these two classes as follows:

#include "aclass.h"
#include "bclass.h"

int main() {
    A a;
    B b;
    a.bar(b);
    return 0;
}

By placing the definition of the class in the header file and including the corresponding header in the file that uses this class file, we can use these two classes correctly in the files that need to use them, thereby solving the compilation error.

To sum up, when we encounter the "error: incomplete type is not allowed" error in C code, we can use forward declaration by placing the definition of the class before the place where this class is used. Or put the definition of the class in the header file to solve this problem. These methods can help us use incomplete types correctly to avoid this compilation error.

The above is the detailed content of Solve the problem of 'error: incomplete type is not allowed' in C++ code. 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