Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Having issues with capability classes/polymorphism

Status
Not open for further replies.

hylian90

MIS
Dec 14, 2004
16
US
Hey, y'all. As the thread title suggests, I'm having trouble with capability classes in the C++ language. Basically, I have a capability class and two derived classes from it. All of these are .hpp header files. The capability class has a virtual method which is overridden in both derived classes. The code is fairly short (it's a test program) so I'll some of it here.

CAPABILITY CLASS HEADER FILE:
Code:
#include <iostream>
using namespace std;

class Combatants
{
    public:
      virtual  int Attack() const { return -1; }  //error
};

FIRST DERIVED CLASS HEADER FILE:
Code:
class Player : public Combatants
{
    public:
      // Constructor & destructor
      Player(int initpAttack, int initpDefend, int initpHP, int initpMP)
      {
            pAttack = initpAttack;
            pDefend = initpDefend;
            pHP = initpHP;
            pMP = initpMP;
      };
      ~Player() { };
      
      // Player attack method
      int Attack( int eHP, int pAttack, int eDefend) const
      {
            cout << "Player attacks!!!" << endl;
            enemyHP = enemyHP ((pAttack * 10)-(eDefend * 5));
            return eHP;
      }
};

SECOND DERIVED CLASS HEADER FILE:
Code:
class Enemy : public Combatants
{
    public:
      // Constructor & destructor
      Enemy(int initeAttack, int initeDefend, int initeHP, int initeMP)
      {
            eAttack = initeAttack;
            eDefend = initeDefend;
            eHP = initeHP;
            eMP = initeMP;
      };
      ~Enemy() { };
      
      // Enemy attack method
      int Attack(int pHP, int eAttack, int pDefend)
      {
            cout << "Enemy attacks!" << endl;
            pHP = pHP - ((eAttack * 10) - (pDefend * 5));
            return pHP;
      }
};

MAIN PROGRAM:
Code:
// Include class headers
#include "combatants_capability.hpp"
#include "player.hpp"
#include "enemy.hpp"


// Main program execution
int main()
{
    // Delcare pointers to initialize instances of player and enemy
    int *pPlayer = new Player(5, 5, 100, 50);
    int *pEnemy = new Enemy(5, 5, 100, 50);
    
    // Player attack method call and HP remaining message
    pPlayer -> Attack(pHP, eAttack, pDefend);
    cout << "Player has " << pHP << " HP remaining.\a" << endl;
    
    // Enemy attack method call
    pEnemy -> Attack(eHP, pAttack, eDefend)
    cout << "Enemy has " << eHP << " HP remaining.\a" << endl;
    
    // Pause system to allow user to see program results
    system("PAUSE");
    
    // Return a 0 to show program executed correctly
    return 0;
}

The problem: My compiler keeps highlighting #include "player.hpp" as an error. But I don't know why!!! I'm using the Dev-C++ environment on Windows XP. Can anyone help me? :-(
 
Looks good to me. Could it be that Player is a reserved keyword on c++ ?

EdwardJS
 
Well, I'm looking at a list of C++ keywords, and I don't see it. >.< Good suggestion, though :)

I have no idea what MIS means
 
It's not anything really stupid and annoying a typo in the file name, e.g. it should be "Player.hpp" instead of "player.hpp"?
 
*goes to check*

Nope, the file names are right, and they're all in the same folder. Could it be that class Player can't access class Enemy's protected variables?[pc3]

I have no idea what MIS means
 
Yup, that was the problem. Player can't access Enemy's variables. Cool, now I know what's wrong!

Code:
//Program to tick off my sister
Programming is officialy a life skill

I have no idea what MIS means
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top