iDevGames Forums
Inheritance issues - Printable Version

+- iDevGames Forums (http://www.idevgames.com/forums)
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: Game Programming Fundamentals (/forum-7.html)
+--- Thread: Inheritance issues (/thread-3239.html)



Inheritance issues - Tekkan - Jun 13, 2007 06:29 PM

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


Inheritance issues - OneSadCookie - Jun 13, 2007 06:34 PM

Code:
#ifndef MyHeaderName
#define MyHeaderName

// header's code goes here

#endif



Inheritance issues - Cochrane - Jun 16, 2007 02:01 AM

Code:
#pragma once



Inheritance issues - Tekkan - Jun 16, 2007 06:30 PM

Thanks, guys. Google had failed me with this information.