Quote:
Originally Posted by dwhitney67
In the Daughter.h header file, the class depends on Mother. Thus you should have included Mother.h within Daughter.h.
|
That is a question of style and better/worse, rather than right/wrong.
I totally agree with you on the answer to this question of better programming style. But I think you may be confusing the OP by phrasing a better style answer as if it were a question of right/wrong.
The original was wrong (not including mother.h from daughter.h
and including daughter.h before mother.h in a cpp. The OP's change to fix only the sequence of include was not wrong, merely inferior style.
Quote:
Originally Posted by michaelinux
So.. is there a rule on how to include header files that i am missing?
|
It isn't really a rule specific to header files. The rule is that the compiler needs to see the declaration or definition of a name
before some kinds of use of that name and must see the definition (just declaration is not good enough)
before other kinds of use of that name.
In your example, it needed the definition of Mother (before its use) and had neither the declaration nor definition.
The header file sequence affects that rule indirectly by affecting the sequence in which the compiler reads things. But nothing about the real rule is specific to header files.
I think, as a matter of good style, every single header file should be coded so that if it were the first header file some cpp file included then it would still be able to compile. In other words the header file includes anything it needs for itself and never depends on the thing that included it including anything else before it.