Say that we have two classes:
// Object.hpp
class Object
{
Handler* h;
Object();
void method();
};
// Object.cpp
Object::Object()
{
h = new Handler();
}
// Handler.hpp
class Handler
{
void update(Object*);
};
// Handler.cpp
void Handler::update(Object* o)
{
o->method();
}
How to link the four files without there being an inclusion error? I've tried all of the ways that I could think of and that I could find online.
Adding #include
in both files will result in this error:
Handler.hpp: error: ‘Object’ has not been declared
update(Object*);
^
If I add a forward declaration of Object in Handler i get this:
Object.hpp: error: ‘Handler’ does not name a type
Handler handler
Including Object in Handler.hpp and forward declaring Handler in Object.hpp gives this:
Object.hpp: error: field ‘h’ has incomplete type ‘Handler’
Handler handler;
^
Meanwhile including Handler in Object.hpp an forward declaring Object in Handler.hpp gives this:
Handler.cpp: error: invalid use of incomplete type ‘class Object’
o->method();
^
I can't seem to figure out the way to include the files in each other. This structure is based on the decoupling component pattern from Robert Nystrom's Game Programming Patterns. Any help is appreciated. Thanks