LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   C++, order of included header files in inheritance? (http://www.linuxquestions.org/questions/programming-9/c-order-of-included-header-files-in-inheritance-924462/)

michaelinux 01-18-2012 01:02 PM

C++, order of included header files in inheritance?
 
I've been trying to learn c++ and when learning inheritance i came up with this problem:

i have 3 files, mother, daughter and the main cpp. Mother and daughter have their respective header files.
i included the daughter.h file in the daughter.cpp file as well as the mother header file. like this:

Daughter.cpp
Code:

#include "Daughter.h"
#include <iostream>
#include "Mother.h"


using namespace std;

Daughter::Daughter(){

}
.
.
.

when i try to compile this, i get an error in my daughter header file.
saying: error: expected class-name before { token

daughter.h
Code:

#ifndef DAUGHTER_H
#define DAUGHTER_H


class Daughter: public Mother // <- Obviously the class name is there!
{

    public:
        Daughter();
};

#endif // DAUGHTER_H

but when i change the order of the included headers in the daughter.h like this:
Code:

#include <iostream>
#include "Mother.h"
#include "Daughter.h"



using namespace std;

Daughter::Daughter(){

}
.
.
.

IT WORKS!!
So.. is there a rule on how to include header files that i am missing?

dwhitney67 01-18-2012 01:26 PM

Yes, you are missing something. In the Daughter.h header file, the class depends on Mother. Thus you should have included Mother.h within Daughter.h.

Mother.h:
Code:

#ifndef MOTHER_H
#define MOTHER_H

class Mother
{
  ...
};

#endif

Daughter.h:
Code:

#ifndef DAUGHTER_H
#define DAUGHTER_H

#include "Mother.h"

class Daughter : public Mother
{
  ...
};

#endif

Daughter.cpp:
Code:

#include "Daughter.h"

Daughter::Daughter()
{
  ...
}

...

Does this clarify everything?

johnsfine 01-18-2012 02:02 PM

Quote:

Originally Posted by dwhitney67 (Post 4578093)
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 (Post 4578067)
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.


All times are GMT -5. The time now is 06:06 AM.