converting base pointer to derived class pointer not working?

Hey guys

I have a small cmd game I'm working on, in the game I have an Item base class and derived classes of Item include Weapon class and Defensive class.

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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
//Item.h
class Item
{
public:
    float getValue(){return value;}
    const std::string getName(){return name;}
    virtual ~Item();
protected:
    float value;
    std::string name;

};



//Character class
class Character
{
public:
    Character(float maxHp,float str,float def);
    void EquipWeapon(Weapon weap); //or this one
    void EquipWeapon(Weapon *weap); //function in question
    //return functions
    float getHp();
    float getMaxHp();
    float getStr();
    float getDef();
    float getLf();
    float getCrit();
    float getBlock();
    float getAgility();
    float getMints();
    void addMints(float amt);
    bool useMints(float amt);
    float getDmg();
    //editing functions
    void healHp(float quan);
    void upMaxHp(float quan);
    void modDmg(float damage);

    //vector stuff
    void addItem(Item item);
    void showItems();
    void findWeapon(std::string name);
private:
    std::vector <Item> inventory; //This is the vector in question.
    Weapon * equipped_weapon; //pointer to equipped weapon
    float mints;
    float xp;
    float dmg;
    float hp;
    float maxHp;
    float str;
    float def;
    float lf;
    float crit;
    float block;
    float agility;
};



I want to allow the player to buy as many items as he wants but only be able to equip a certain number of items. For that I have created a equipWeapon(Weapon *weap) function inside of Character.h

Now here is the issue.
I have some options for the user, if he wants to equip an item there will be an interface for it.

For example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//user enters name of weapon he wishes to equip into string variable
//use that name in function ->

void Character::findWeapon(std::string name)//in Character.h
{
    for(int i = 0; i < inventory.size(); i++)
        if(inventory[i].getName() == name)
            Item *ptr = &inventory[i]; //Base class pointer to object
            Weapon<int> * weaponPtr = dynamic_cast<Weapon<int>* >(ptr);
            //okay? here I'm having trouble, make that pointer point to
            //derived weapon class object which it is
            //the vector is simply used to store all items (instead of having
            //multiple vectors)
            EquipWeapon(weaponPtr); //call on class that actually equips the
            //item and modifies the damage of the user
}


void Character::EquipWeapon(Weapon *weap) //but this doesn't work
{
    dmg = 1 + (1.2 * str);
    dmg += weap->getDmg();
}


I get an error on line 9 that says "weapon is not a template" and am unable to do anything after that. I hope I was clear in my question, please let me know if you need further information.
Weapon<int> What int stands for? How you defined Weapon class?
I read about dynamic_cast here http://stackoverflow.com/questions/18873871/c-converting-a-base-class-pointer-to-a-derived-class-pointer, and I tried to do the same.
I don't know ;/
Last edited on
The error is highly descriptive. The Weapon class is not a template, so you cannot supply template parameters (the values inside the <>). You copied and pasted too faithfully from that example; you should replace Weapon<int> by simply Weapon as that is the name of the type.
Does you type named Weapon<int> or just Weapon? Use correct type name.
I see, I'm sorry that is a silly mistake, I read the question on stack overflow and thought that was simply the way dynamic casts were done since I didn't see any int types inside of the question right away.


Other than that, is the conversion okay?
Do not forget to check pointer returned by dynamic_cast. It returns nullptr if object cannot be cast to that type.
yessir :D
Topic archived. No new replies allowed.