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
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