It would help if you supplied a minimal, compilable example. The few snippets you show don't quite get across what you're trying to do and how you're trying to do it.
ok well I am using this code. the way I under stand things is that u have your base class and the variables that (Items) all the other classes use. Then u have the other classes which I have weapon (which is a item) and ill have armor(which is a item) and maybe later (potions ) but with the weapons class I put in that it has a private: int basedamage, mindamage.
because armor and potions don't have damage.
some help would be greatly appreciated and try to solve my question first and if there is other things u see a suggestion would be good as I have only been at this for about 2 weeks with learning it and trying to have some fun.
void equipItem(vector<Items*> &Item, Player* play, string input, string Inventory[])
{
cout << "Now in equip\n";
// check item in inventory
string str = input;
str.erase(0,6); // erase 'equip '
unsignedint itemIsThere = -1;
if (str.size() > 3) // make sure there is at least 4 letters to compare against inventory
{
// = itemInInventory(Inventory, str);
//string completeName = Inventory[itemIsThere];
int number = itemInventoryVectorNum(Item, str);
if (number != -1)
{
cout << Item[number]->getIType() << endl;
if (Item[number]->getIType() == "weapon")
{
cout << Item[number]->getISlot()<< endl;
if (Item[number]->getISlot() == "righthand")
{
if (play->getRightHandFree())
{
//need to equip item
play->setRighthand(Item[number]->getIName());
play->setRightHandFree(false);
Item[number]->setILoc(EQUIPPED);
removeFromInventory(Inventory, Item[number]->getIName());
//add bonuses to character
play->setWBaseDam(weapon::wBaseDamage()[number]-); // I created weapons and if im understanding the way things work then I guess things could be way outta wack
play->setWMinDam(1);
return;
}
else
{
cout << "There is a " << play->getRighthand() << " equipped. Would you like to swap items? y/n?\n";
bool answer = getYesNo();
{
if (answer)
{
cout << "Lets swap that item\n";
//remove item from equip
play->setRighthand("");
//play->setWBaseDam();
//remove bonuses
//add new item to equip
//add bonuses
return;
}else cout << "Left " << play->getRighthand() << " equipped.\n"; return;
}
}
}
weapon.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#pragma once
#include "items.h"
class weapon :
public Items
{
private:
int wBaseDamage, wMinDamage;
public:
weapon(void);
virtual ~weapon(void);
void WAttack();
void setBaseDam(int base);
void setMinDam(int min);
int getBaseDam();
int getMinDam();
};
#include <iostream>
#include <string>
struct Item
{
Item(const std::string& name) : _name(name) {}
std::string name() const { return _name; }
private:
std::string _name;
};
struct Weapon : public Item
{
Weapon(const std::string& name, int damage) : Item(name), _damage(damage) {}
void setDamage(int dam) { _damage = dam; }
int getDamage() const { return _damage; }
private:
int _damage;
};
int main()
{
Weapon weapon("Axe", 6);
Item* iptr = &weapon;
// Your question seems to be: I have a pointer to Item, how do I
// access the get or setDamage methods of Weapon?
// Well, if we KNOW that iptr points to a Weapon object, we can
// use a static cast.
static_cast<Weapon*>(iptr)->setDamage(10);
}
However, it isn't very likely that you will know in generic code what the derived type of the pointer you have is pointing to. So, we'll add a virtual destructor to the Item class to make the Item class and any class that derives from it polymorphic, then we may use a dynamic cast to determine the type of the object pointed to.