...

View Full Version : Multiple classes inheriting parent



TurkeyMaster77
11-18-2008, 07:19 AM
Working in C++ here. Here's the simplified problem: I need multiple classes to all inherit from the base class, let's say "Fruit" for now. I keep all of my classes in separate files (.cpp and .h files), where .h has mainly prototypes and .cpp contains function defs. I'm having issues with the #include statements. Apple works fine, but when I incorporate Orange, I get errors concerning class redifinition? I can't figure out where to put the include statements, because putting it in my driver.cpp throws the same errors!

Driver.cpp:

#include <string>
#include "Apple.h"
#include "Orange.h"
using namespace std;

int main () {
return 0;
}

Fruit.h:

#include <string>
using namespace std;

class Fruit {
public:
Fruit () {};
protected:
string name;
string color;
};

Apple.h:

#include "Fruit.h"
using namespace std;

class Apple : public Fruit {
public:
Apple () {};
};

Orange.h:

#include "Fruit.h"
using namespace std;

class Orange : public Fruit {
public:
Orange () {};
};

oracleguy
11-18-2008, 10:00 AM
It is because you are not protecting your includes with preprocessor definitions. Basically when you include orange.h and apple.h they each will bring in a copy of fruit.h which then leads to the code in fruit.h being repeated.

An example of protecting it would be for your fruit.h:


#include <string>
using namespace std;

#ifndef FRUIT_H
#define FRUIT_H
class Fruit {
public:
Fruit () {};
protected:
string name;
string color;
};
#endif



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum