class not declared in scope – even though .h was included

The common circular include problem occurs with:
a.h

#ifndef A_H
#define A_H
#include "b.h"
// do something requiring content from b.h
#endif  

b.h

#ifndef B_H
#define B_H
#include "a.h"
// whatever
#endif  

If some cpp includes “a.h” you might get away with it (if b.h didn’t really need a.h). But if that cpp instead includes b.h then b.h includes a.h BEFORE declaring things needed by a.h. Then a.h tries to include b.h but the include guard blocks that, so the compiler processes a.h without the declarations from b.h and fails.

That common problem is often masked by another layer, as it was in your example: todoitem.h included “todolist.h”, even though it didn’t really need it, then “todolist.h” included “intemmonitor.h” which needed “todoitem.h” but failed to include it due to the include guard.

Leave a Comment