LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 01-18-2012, 02:02 PM   #1
michaelinux
Member
 
Registered: Apr 2009
Location: El Salvador
Distribution: Slackware -current
Posts: 166

Rep: Reputation: 17
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?
 
Old 01-18-2012, 02:26 PM   #2
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,523

Rep: Reputation: 332Reputation: 332Reputation: 332Reputation: 332
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?
 
Old 01-18-2012, 03:02 PM   #3
johnsfine
Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,138

Rep: Reputation: 1127Reputation: 1127Reputation: 1127Reputation: 1127Reputation: 1127Reputation: 1127Reputation: 1127Reputation: 1127Reputation: 1127
Quote:
Originally Posted by dwhitney67 View Post
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 View Post
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.

Last edited by johnsfine; 01-18-2012 at 03:13 PM.
 
  


Reply

Tags
c++, inheritance, programing


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to check missing header files included from another header file adisan82 Linux - Software 1 01-28-2011 04:57 AM
Copy folders + included mp3's in order CQ1ST Linux - Newbie 7 03-22-2010 09:20 PM
How to compile a C program in Glade which is included the <pcap.h> header file. swaviswa Programming 0 03-21-2004 08:47 AM
c header files in linux in place of header files in windows? harun_acs Programming 1 03-17-2004 03:24 AM
math.h and other included header files ecc6 Slackware 1 04-19-2003 03:15 PM


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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration