Inheritance issues
I have run into a ridiculously frustrating problem that I feel there must be a basic solution to. Basically, say I want two classes to extend the same base class, but each class has its own .cpp and .h files. If I try to include both of the child class' header files, I get an error saying that I'm trying to redefine the base class.
This is a shortened down version of what I'm trying to do:
AnimateObject.h:
Player.h:
Enemy.h:
Now in main.cpp, I obviously want to be able to create both a Player and an Enemy object, but I can't, because if I try:
I get an error stating "previous redefinition of struct State" and "previous redefinition of class AnimateObject".
There has to be a simple way around this, right?
This is a shortened down version of what I'm trying to do:
AnimateObject.h:
Code:
struct State
{
bool poisoned;
bool on_fire;
};
class AnimateObject
{
public:
float x, y, vx, vy;
float health;
State status;
AnimateObject();
AnimateObject(float x, float y, float vx, float vy);
// other stuff ignored
};Player.h:
Code:
#include "AnimateObject.h"
class Player : public AnimateObject
{
public:
Player() : AnimateObject() {};
Player(float x, float y) : AnimateObject(x, y, 0, 0) {};
};Enemy.h:
Code:
#include "AnimateObject.h"
class Enemy : public AnimateObject
{
public:
Enemy() : AnimateObject() {};
Enemy(float x, float y) : AnimateObject(x, y, 0, 0) {};
};Now in main.cpp, I obviously want to be able to create both a Player and an Enemy object, but I can't, because if I try:
Code:
#include "Player.h"
#include "Enemy.h"I get an error stating "previous redefinition of struct State" and "previous redefinition of class AnimateObject".
There has to be a simple way around this, right?
Code:
#ifndef MyHeaderName
#define MyHeaderName
// header's code goes here
#endifCode:
#pragma once
Possibly Related Threads...
| Thread: | Author | Replies: | Views: | Last Post | |
| Inheritance | merrill541 | 4 | 2,527 |
Feb 23, 2009 07:37 AM Last Post: ozirus |
|

