Polymorphic constructor?

I have an abstract base class (which means that an object of it is never instantiated, correct?), and another class that inherits from it.

Entity.h
1
2
3
4
5
6
7
8
9
class Entity
{
    public:
    Entity(std::list<Action*> lskills, std::string sName="Unnamed", sf::Vector2f v2fPos=sf::Vector2f(0,0),
           /* etc, etc */ );
    virtual ~Entity();

    // etc..
};


Player.h
1
2
3
4
5
6
class Player : public Entity
{
    public:
    Player();
    ~Player();
};


My compiler is telling me that there is no matching function calls for Player() to Entity::Entity(), which makes sense because I haven't defined it. However, does this mean that I have to include this argument list for Player()? I had wanted to be able to let the Entity() constructor deal with initializing members common to both Player and other classes inheriting from Entity, but I guess that wouldn't work? Or am I just taking the wrong approach?
However, does this mean that I have to include this argument list for Player()?


No, it means you must supply all the necessary parameters for the Entity() constructor:

1
2
3
4
Player::Player()
 : Entity( a_list_of_skills, "Name of the Player", etc, etc )
{
}


or you make a different Entity constructor that doesn't need those params.


Really, though, it doesn't seem like you should give all those things to the Entity ctor. Containers in particular (like that list of skills) make things particularly hard to construct.
Thanks Disch, I really shouldn't have the list there.

So would it look like this?

Player::Player()
: Entity("Unnamed", sf::Vector2f(0,0), 100, 10, 3)

Then if I wanted to do Player player("name", etc) I would need to overload the Player ctor?



Also, to avoid making another topic, can someone tell me why I'm getting an error about a line that I've commented out?

1
2
3
4
void Entity::AddSkill(Action* skill)
{
    skills.push_back(skill);
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
class Action;

class Entity
{
    public:
    Entity(std::string sName="Unnamed", sf::Vector2f v2fPos=sf::Vector2f(0,0),
           unsigned int uhealth = 100, unsigned int ustrength = 10,
           unsigned int uskillpoints = 3);
    virtual ~Entity();

    virtual void Move(sf::Vector2f dydx) = 0;
    virtual void TakeDamage(int dmg) = 0;
    virtual int  CheckHealth() = 0;
    virtual int  Attack(Entity* target) = 0; // Attack a single target.
    virtual int  Attack(std::list<Entity*> targets) = 0; // Attack multiple targets
    virtual std::list<Action*> CheckSkills();

    //virtual void AddSkill(Action* skill); Error here... but it's COMMENTED OUT?

    std::string name;
    sf::Vector2f pos;

    protected:
    unsigned int health;
    unsigned int strength;
    unsigned int skillpoints;
    std::list<Action*> skills;

    virtual bool Init() = 0; // We will need to define separate Init()
                             // functions for each class that we create, since
                             // they will have their own static sf::Image instances.

};


error: prototype for 'void Entity::AddSkill(Action*)' does not match any in class 'Entity' |
error: candidate is: virtual void Entity::AddSkill(const Action*) | it's commented out..

did you comment out the AddSkill function in the cpp file? cuz if not then it would most likely give you an error, i'm guessing on that, lol.
Topic archived. No new replies allowed.