Inheritance issues

Tekkan
Unregistered
 
Post: #1
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?
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #2
Code:
#ifndef MyHeaderName
#define MyHeaderName

// header's code goes here

#endif
Quote this message in a reply
Member
Posts: 114
Joined: 2005.03
Post: #3
Code:
#pragma once
Quote this message in a reply
Tekkan
Unregistered
 
Post: #4
Thanks, guys. Google had failed me with this information.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Inheritance merrill541 4 2,722 Feb 23, 2009 07:37 AM
Last Post: ozirus