Hey guys. I'm having a problem with an rpg that I have been working on, being fairly new to inheritance. I have 3 character types that I have, each inheriting from a base Character class. I initialize an object on the global scope: Character *p_Player;
Once the player chooses the class they would like to test, as this is all they can do at this point, I assign the pointer to the corresponding class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
classNum = getClass();
//set the pointer to the correct class
switch (classNum)
{
case 1:
p_Player = new Warrior();
p_Player -> ShowRecomendedStats(WARRIOR_STATS);
break;
case 2:
p_Player = new Mage();
p_Player -> ShowRecomendedStats(MAGE_STATS);
break;
case 3:
p_Player = new Archer();
p_Player -> ShowRecomendedStats(ARCHER_STATS);
break;
}
When I try to access the member functions of each subclass, I get a compile error. Here is where I try to access them:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
void specialAttack(int classChoice)
{
switch (classChoice)
{
case 1:
p_Player -> Slam();
break;
case 2:
p_Player -> Fireball();
break;
case 3:
p_Player -> Shoot();
break;
}
return;
}
Any ideas on how I could work this out so that I can access these functions? Thanks.
If you are using polymorphism then you should in your base class Character to declare functions you want to access as virtual.
In your case the compiler says that static type Character has not such functions that you are trying to call.
It says "class Character has no member function named Slam()/Fireball()/Shoot()". Those three functions are defined in the Warrior, Mage, and Archer classes respectively.