Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invalid use of incomplete type struct, even with forward declaration

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

like image 610
robev Avatar asked Apr 04 '11 19:04

robev


People also ask

Can you forward declare a struct?

In C++, classes and structs can be forward-declared like this: class MyClass; struct MyStruct; In C++, classes can be forward-declared if you only need to use the pointer-to-that-class type (since all object pointers are the same size, and this is what the compiler cares about).

What is a forward declaration in C++?

Forward Declaration refers to the beforehand declaration of the syntax or signature of an identifier, variable, function, class, etc. prior to its usage (done later in the program). Example: // Forward Declaration of the sum() void sum(int, int); // Usage of the sum void sum(int a, int b) { // Body }


2 Answers

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 #include the whole class definition for that header. This can improve your compile times. But the implementation for that header is almost guaranteed to need to #include the 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.

like image 173
Max Lybbert Avatar answered Sep 28 '22 03:09

Max Lybbert


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

like image 30
Šimon Tóth Avatar answered Sep 28 '22 04:09

Šimon Tóth