C++ 不完整类型结构的无效使用,即使有前向声明

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5543331/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 18:24:18  来源:igfitidea点击:

Invalid use of incomplete type struct, even with forward declaration

c++forward-declaration

提问by robev

I'm aware of circular dependencies, but even with forward declarations I get this area. What am I doing wrong?

我知道循环依赖,但即使有前向声明我也得到了这个区域。我究竟做错了什么?

// facility.h
class Area;

class Facility {
public:
    Facility();
    Area* getAreaThisIn();
    void setAreaThisIsIn(Area* area);
private:
    Area* __area;
};

// facility.cpp
#include "facility.h"
#include "area.h"
{ ... }

// area.h
class Facility;
class Area {
public:
    Area(int ID);
    int getId();

private:
    std::list<Facility*> _facilities;
};

// area.cpp
#include "area.h"
#include "facility.h"

So this compiles fine, but if I do

所以这编译得很好,但如果我这样做

// foo.h
#include "facility.h"
class Foo { .. };

// foo.cpp
#include "foo.h"
void Foo::function() {
    Facility* f = new Facility();
    int id = f->getAreaThisIsIn()->getId();

When I get invalid use of incomplete type struct Area

当我得到 invalid use of incomplete type struct Area

采纳答案by ?imon Tóth

For Facility* f = new Facility();you need a full declaration, not just forward declaration.

因为Facility* f = new Facility();您需要一个完整的声明,而不仅仅是前向声明。

回答by Max Lybbert

To clarify: a forward declaration allows you to operate on an object if very limited ways:

澄清:前向声明允许您在非常有限的情况下对对象进行操作:

struct Foo; // forward declaration

int bar(Foo* f); // allowed, makes sense in a header file

Foo* baz(); // allowed

Foo* f = new Foo(); // not allowed, as the compiler doesn't
                    // know how big a Foo object is
                    // and therefore can't allocate that much
                    // memory and return a pointer to it

f->quux(); // also not allowed, as the compiler doesn't know
           // what members Foo has

Forward declarations can help in some cases. For instance, if the functions in a header only ever take pointers to objects instead of the objects, then you don't need to #includethe whole class definition for that header. This can improve your compile times. But the implementation for that header is almost guaranteed to need to #includethe relevant definition because you're likely going to want to allocate those objects, call methods on those objects, etc. and you need more than a forward declaration to do that.

在某些情况下,前向声明会有所帮助。例如,如果头文件中的函数只使用指向对象而不是对象的指针,那么您就不需要#include该头文件的整个类定义。这可以改善您的编译时间。但是该标头的实现几乎可以保证需要#include相关定义,因为您可能想要分配这些对象,调用这些对象的方法等,并且您需要的不仅仅是一个前向声明来做到这一点。

回答by Frank Schmitt

Did you #include both area.h and facility.h in foo.cpp (assuming this is the file where you get the error)?

您是否在 foo.cpp 中同时#include了area.h和facility.h(假设这是您收到错误的文件)?