inheritance problem - base class redifinition

B

Budi Sofian

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
 
J

John Carson

Budi Sofian said:
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.
 
S

Sumit Rajan

Budi Sofian said:
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.
 
B

Budi Sofian

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

Regards, Budi Sofian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,167
Messages
2,570,910
Members
47,453
Latest member
MadelinePh

Latest Threads

Top