So, I am doing a bit of C++ programming, and have run into a problem. I am trying to do something that I thought was possible, but have never done before.
I have this code:
#include "Files.h"
#include "Tileset.h"
class Entity
{
public:
Entity(){};
~Entity(){};
bool CanMove;
bool IsAnimate;
bool IsLiving;
int XPosition;
int YPosition;
int XWindowPos;
int YWindowPos;
int AbsolutePosition;
int TileID;
float Velocity = 0;
int MoveDirection = -1;
sf::Sprite EntitySprite;
void UpdateSpritePosition()
{
EntitySprite.setPosition(XWindowPos, YWindowPos);
}
void SetSprite(int aTileID, Tileset &SpriteTileset)
{
TileID = aTileID;
EntitySprite.setTexture(SpriteTileset.TilesetTexture);
EntitySprite.setTextureRect(SpriteTileset.GetCellBounds(TileID));
};
};
#include "Entity.h"
#include "Files.h"
class Human : public Entity
{
public:
Human(){};
~Human(){};
bool ReqBreathe = 1;
bool ReqEat = 1;
bool ReqDrink = 1;
bool ReqSleep = 1;
bool ReqBlood = 1;
float Health = 100.0;
float MoveSpeed = 10;
};
#include "Human.h"
class Player : public Human
{
public:
Player(){};
~Player(){};
void Configure()
{
CanMove = 1;
IsAnimate = 1;
IsLiving = 1;
}
};
#include "Player.h"
#include "Files.h"
class EntityManager
{
public:
EntityManager(){};
~EntityManager(){};
std::map <int, Entity> EntityContainer;
int GetCurrentKey()
{
return CurrentKey;
};
int GetEntityCount()
{
return EntityCount;
};
void AddEntity(Entity &NewEntity)
{
EntityContainer.insert(std::make_pair(EntityCount, NewEntity));
CurrentKey++;
EntityCount++;
};
void RemoveEntity(int EntityKey)
{
EntityContainer.erase(EntityKey);
EntityCount++;
};
void ClearEntities(int EntityID)
{
EntityContainer.clear();
EntityCount = 0;
};
private:
int EntityCount = 0;
int CurrentKey = 0;
};
The main area of interest is the declaration of the map "EntityContainer" in EntityManager.h. My thoughts were that since the class "Human" is derived from the class "Entity" and "Player" is derived from "Human", that I would be able to store both of those in the map.
Is there any way to declare a std::map so that it can contain both the class object it is declared to hold and any class object derived from that class?
For example, I try to do something like this:
EntityManager EMan;
EMan.AddEntity(new Player);
...and receive an error stating that the map was declared to hold "Entity" class objects, not "Player" class objects. What am I doing wrong here?