How do I solve a circular header file include, where A.h includes B.h, B.h includes C.h, and C.h includes A.h and B.h? (There is one class in each file. And no, a forward declaration of C in B.h doesn't help.)
I am working on a scheduler and the problem involves three classes: Task, Event and Schedule. Schedules contain Events, which in turn contain Tasks, in the following way:
(Both Schedule and Event invokes methods on their relevant members.)Code:class Schedule { list<const Event*> events; /* ... */ }; class Event { const Task task; /* ... */ }; class Task { /* ... */ };
However, I run into problems when I try to add a method for determining whether a Task can be added to a Schedule. I think the method should be in the Task class, since it mostly uses data from that class, but since it refers to both Schedule and Event, it introduces two circular dependencies.
The easiest way to remove the need for circular includes would be to use a forward declaration in Event.h for the Task class, but for some reason this doesn't work. The compiler seems to object to me doing just about anything with the Task class in Event.h.Code:bool Task::isAppendableTo(Schedule& schedule) { /* ... */ Event* event; /* ... */ }
Thankful for any suggestions,
Einar