By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
426,034 Members | 1,672 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 426,034 IT Pros & Developers. It's quick & easy.

inheritance problem - base class redifinition

P: n/a
Greetings everyone,

I created one base class called Animal and two derivative class (from
Animal) called Fish and Mammal. I separated the each class definition and
its member functions definition into two files (e.g. "animal.h" and
"animal.cpp").

"animal.h" contains the following code:
#ifndef ANIMAL_H
#define ANIMAL_H
#endif

class Animal
{
......
};

"fish.h" is as follow:
#ifndef FISH_H
#define FISH_H
#endif
#include "animal.h"

class Fish : public Animal
{
......
};

"mammal.h" is as follow:
#ifndef MAMMAL_H
#define MAMMAL_H
#endif
#include "animal.h"

class Mammal : public Animal
{
......
};

I have compiled the "animal.cpp", "fish.cpp", "mammal.cpp" and they didn't
generate any errors. However, when I tried to use the Fish and Mammal in
main(), the Microsoft Visual C++ generates the "base class redefinition"
error. The content of "main.cpp" is:

#include <iostream.h>
#include <string.h>
#include "fish.h"
#include "mammal.h"

int main()
{
Fish f;
Mammal m;
.......
}

The interesting thing is that when I tried to omit the [#include "animal.h"]
in "mammal.h", Visual C++ doesn't generate any error and the program works
fine. However, I don't think this is the right solution for the problem. Can
anyone help me with this? Any help is appreciated. Thank you.

Regards, Budi Sofian
Jul 22 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
"Budi Sofian" <so*****@netspace.net.au> wrote in message
news:c6***********@otis.netspace.net.au
Greetings everyone,

I created one base class called Animal and two derivative class (from
Animal) called Fish and Mammal. I separated the each class definition
and its member functions definition into two files (e.g. "animal.h"
and "animal.cpp").

"animal.h" contains the following code:
#ifndef ANIMAL_H
#define ANIMAL_H
#endif


#endif should go at the very end of the header file. In that way, the header
file contents are skipped entirely if ANIMAL_H is already #defined, which is
the whole point of the exercise. Make the same change for all header files.
--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)

Jul 22 '05 #2

P: n/a
Thank you, John. I have changed my code according to your suggestion and the
problem has been solved.

Regards, Budi Sofian

"John Carson" <do***********@datafast.net.au> wrote in message
news:40******@usenet.per.paradox.net.au...
"Budi Sofian" <so*****@netspace.net.au> wrote in message
news:c6***********@otis.netspace.net.au
Greetings everyone,

I created one base class called Animal and two derivative class (from
Animal) called Fish and Mammal. I separated the each class definition
and its member functions definition into two files (e.g. "animal.h"
and "animal.cpp").

"animal.h" contains the following code:
#ifndef ANIMAL_H
#define ANIMAL_H
#endif
#endif should go at the very end of the header file. In that way, the

header file contents are skipped entirely if ANIMAL_H is already #defined, which is the whole point of the exercise. Make the same change for all header files.

--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)

Jul 22 '05 #3

P: n/a

"Budi Sofian" <so*****@netspace.net.au> wrote in message
news:c6***********@otis.netspace.net.au...
Greetings everyone,

I created one base class called Animal and two derivative class (from
Animal) called Fish and Mammal. I separated the each class definition and
its member functions definition into two files (e.g. "animal.h" and
"animal.cpp").

"animal.h" contains the following code:
#ifndef ANIMAL_H
#define ANIMAL_H
#endif

class Animal
{
.....
};


The #endif needs to come at the end. So you need to have something like:

//animal.h
#ifndef ANIMAL_H
#define ANIMAL_H

class Animal {
//....
};

#endif

You will need to do the same for fish.h and mammal.h.

Regards,
Sumit.

Jul 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.