简体   繁体   English

C ++这是一个不完整的类型吗?

[英]C++ How is this an Incomplete Type?

Here is the code 这是代码

#include <iostream>

#include "ClassA.h"

ClassA* class_a = new ClassA();

int main() {



    return 0;
}

Class A A级

#ifndef CLASSA_H
#define CLASSA_H

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

class ClassA
{
public:
    ClassA()
    {
        ClassB classB = new ClassB();

        std::cout << "End of constructor" << std::endl;
    }

    void ClassA::DoSomething( void )
    {
        std::cout << "ClassA  DoSomething";
    }
};


#endif  /* CLASSA_H */

Class B B级

#ifndef CLASSB_H
#define CLASSB_H

class ClassA;

extern ClassA* class_a;

class ClassB
{

public:

    ClassB ()
    {
        ::class_a->DoSomething();
        std::cout << "ClassB constructor" << std::endl;
    }

};


#endif  /* CLASSB_H */

In ClassB 's constructor I get a compile error at ::class_a->DoSomething(); ClassB的构造函数中,我在::class_a->DoSomething();::class_a->DoSomething();编译错误::class_a->DoSomething(); saying member access into incomplete type "ClassA" . member access into incomplete type "ClassA" But how is ClassA incomplete when it is parsing ClassB's constructor? 但是,在解析ClassB的构造函数时,ClassA如何不完整? When it is going through ClassA, does it get to the type ClassB in it's constructor and then hope over to that file? 通过ClassA时,它是否到达构造函数中的ClassB类型,然后希望转到该文件?

It's an incomplete type because you have not included the header for ClassA in the ClassB header but you're trying to use the ClassA instance. 这是一种不完整的类型,因为您没有在ClassB标头中包含ClassA标头, 但您尝试使用ClassA实例。 In other words, the compiler knows there is a ClassA thanks to the forward declaration, which is sufficient to declare a pointer, but the type is incomplete and cannot be used because the compiler hasn't seen the declaration of the type. 换句话说,由于前向声明,编译器知道有一个ClassA ,足以声明一个指针,但是类型不完整,无法使用,因为编译器没有看到该类型的声明。

You need to make a source file (eg, ClassB.cpp ), include the ClassA header in the source file, and move the ClassB constructor's implementation to the source file. 您需要制作一个源文件(例如ClassB.cpp ),在源文件中包含ClassA标头,然后将ClassB构造函数的实现移到源文件中。

Let's look at this from the point of the compile after it has processed the includes: 让我们从编译处理完includes之后来看:

#line 1 main.cpp
//#include <iostream>
/* Text of iostream inserted here */
//#include "ClassA.h"
#line 1 ClassA.h

//#ifndef CLASSA_H (it isn't defined)
#define CLASSA_H

//#include <iostream>
/* Almost nothing here because iostream has already been included */

//#include "ClassB.h"
#line 1 ClassB.h
//#ifndef CLASSB_H (again, it isn't defined)
#define CLASSB_H

class ClassA;

extern ClassA* class_a;

class ClassB
{

public:

    ClassB ()
    {
        ::class_a->DoSomething();

Right. 对。 So we compiled main.cpp, which included ClassA.h which included ClassB.h. 因此,我们编译了main.cpp,其中包括ClassA.h,其中包括ClassB.h。 At the point we have got to here, ClassA is not defined. 至此,尚未定义ClassA

Move (at least) one of the constructors into a separate .cpp file. 将(至少)一个构造函数移动到一个单独的.cpp文件中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM