473,414 Members | 1,751 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

inheritance problem - base class redifinition

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
3 1659
"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
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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Dan Bullok | last post by:
I have a couple of classes: class Base: ... class Sub(Base): ... I want to subclass Base and Sub, i.e. define classes: class MyBase(Base):
0
by: John Hunter | last post by:
I am using pycxx 5.2.2 to implement some extension code and have a problem relating to inheritance. I have a pure virtual base class and two concrete derived classes. In the code below, everthing...
4
by: JKop | last post by:
I'm starting to think that whenever you derive one class from another, that you should use virtual inheritance *all* the time, unless you have an explicit reason not to. I'm even thinking that...
7
by: Mahesh | last post by:
Hello, I am having a query about inheritance. I have a virtual base class that is derived by to other classes that are hirarchially at the same level. But I still need to ensure that only a...
45
by: Ben Blank | last post by:
I'm writing a family of classes which all inherit most of their methods and code (including constructors) from a single base class. When attempting to instance one of the derived classes using...
1
by: relient | last post by:
Hi, I just started the chapter on "Inheritance" and I'm a bit confused so I have three questions: first, here's the code: using System; using System.Collections.Generic; using System.Text; ...
23
by: Dave Rahardja | last post by:
Since C++ is missing the "interface" concept present in Java, I've been using the following pattern to simulate its behavior: class Interface0 { public: virtual void fn0() = 0; };
12
by: Massimo | last post by:
Hi to all, I'm facing a problem in a particularly complex inheritance hierarchy, and I'd like to know what the standard says about it and if my compiler is correct in what it does. I have two...
3
by: Jess | last post by:
Hello, I've been reading Effective C++ about multiple inheritance, but I still have a few questions. Can someone give me some help please? First, it is said that if virtual inheritance is...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.