Sign in to follow this  

[SOLVED] having trouble getting two classes to interact..

This topic is 3464 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am making a simple text-based RPG. I have two classes, Player and Mob. Each of them have their own attack() and takedmg() commands. For example, when the Player::attack() command is called, it calls the Mob::takedmg() function (and vice-a-versa). Here are my classes layed out:
//player.h
class Player
{
public:

     Player(int hp = 100, int xp = 0, int atk = 17);

     int getHP() const;
     int getXP() const;
     int getATK() const;

     bool attack(Mob& mob);
     void takeDmg(int& dmg);

private:

     int m_HP;
     int m_XP;
     int m_ATK;

};

//mob.h
class Mob
{
public:

     Mob(int hp = 50, string name = "Giant Roach", int attack = 9); //defaults

     int getHP() const;
     string getName() const;
     int getAttack() const;

     int attack(Player& player);
     int takeDmg(int& dmg);

private:

     int m_HP;
     string m_Name;
     int m_Attack;

};


//main.cpp
#include <iostream>
#include <string>
#include <vector>
#include <cstdlib>
#include <ctime>

using namespace std;

#include "player.h"
#include "mob.h"

Player::Player(int hp, int xp, int atk):
                   m_HP(hp),
                   m_XP(xp),
                   m_ATK(atk)
{
     cout << "\t\t-- Player created with " << getHP() << " HP, " << getXP() << " XP, and " << getATK() << " ATK" << endl << endl;
}
int Player::getHP() const
{
     return m_HP;
}
int Player::getXP() const
{
     return m_XP;
}
int Player::getATK() const
{
     return m_ATK;
}
void Player::takeDmg(int& dmg)
{
     m_HP -= dmg;
     cout "You take " << dmg << " damage!" << endl;
}

bool Player::attack(Mob& mob)
{
     cout << "\tYou deal " << getATK() << " damage to " << mob.getName() << endl;

     if (mob.takeDmg(m_ATK) <= 0) //killing blow
     {
          cout << "\tYou killed " << mob.getName() << "!" << endl << endl;
          return 0;
     }
     else //non-killing blow
     {
          cout << "\t" << mob.getName() << " now has " << mob.getHP() << "HP" << endl << endl;
          return 1;
     }
}

Mob::Mob(int hp, string name, int attack):
             m_HP(hp),
             m_Name(name),
             m_Attack(attack)
{
     cout << "\t\t-- " << getName() << " created with " << getHP() << " HP" << endl << endl;
}
int Mob::getHP() const
{
     return m_HP;
}
string Mob::getName() const
{
     return m_Name;
}
int Mob::getAttack() const
{
     return m_Attack;
}
int Mob::takeDmg(int& dmg)
{
     m_HP -= dmg;
     return m_HP;
}
int Mob::attack(Player& player)
{
     player.takeDmg(m_Attack);
     cout << "\t" << getName() << " attacks you for " << getAttack() << " damage" << endl;
     cout << "\tYou now have " << player.getHP()<< " HP" << endl << endl;
     return getAttack();
}


void showMenu();
void showEncounterMenu(Mob& mob);
void display(const vector<string>& inventory);

int main()
{
     int cmd;
     int playerDmg = 14, playerHP = 100;
     int act = 0; //0 = adventure, 1 = encounter
     
     Mob mob;
     Player player;
     vector<string> inventory;    

     inventory.push_back("sword");
     inventory.push_back("armor");
     inventory.push_back("shield");
     
     do
     {
         switch (act)
         {
             case 0: //adventuring
         
                showMenu();
                  
                cin >> cmd;
                cout << endl;
                  
                switch (cmd)
                {
                    case 0:
                        break;
                    case 1:                 
                        mob = Mob();
                        cout << "\tYou find a " << mob.getName() << "!" << endl << endl;
                        act = 1;
                        break;
                    case 2:
                        display(inventory);
                        break;
                }   
                
                break;

            case 1: //encounter
        
            showEncounterMenu(mob);
            
            cin >> cmd;
            cout << endl;
            
            switch (cmd)
            {
                case 1: 
                    if (!player.attack(mob)) act = 0; //if killing blow, end the encounter
                    else mob.attack(player); //else mob attacks back
                    break;
                case 2:
                    act = 0; //ends the encounter
                    break;
                default:
                    cout << "Invalid command" << endl;
            }
                   
            break;

        default:
            cout << "Invalid command" << endl;
        }
    }
    while (cmd != 0);
    
       
    return 0;
}

void showMenu()
{
     cout << "Enter a command:" << endl;
     cout << "================" << endl;
     cout << "1 - Explore" << endl;
     cout << "2 - Show Inventory" << endl;
     cout << "0 - Exit" << endl;
     cout << endl;
}

void showEncounterMenu(Mob& mob)
{
     cout << "Enter a command:" << endl;
     cout << "================" << endl;
     cout << "1 - Attack " << mob.getName() << endl;
     cout << "2 - Leave it alone" << endl;
     cout << endl;
}

void display(const vector<string>& vec)
{
     cout << "-------------" << endl;
     cout << "Inventory [" << vec.size() << "]" << endl;
     cout << "-------------" << endl;
     for (vector<string>::const_iterator iter = vec.begin(); iter != vec.end(); ++iter)
         cout << *iter << endl;
     cout << endl;
}


Now for some reason, im getting an error on the following line in my main.cpp:
#include "player.h"
error says "no `bool Player::attack(Mob&)' member function declared in class `Player' " and "'class Player' has no member named 'attack' " i don't understand what i'm doing wrong! please help thanks in advance! [Edited by - immanuelx2 on June 23, 2008 1:01:17 PM]

Share this post


Link to post
Share on other sites
Quote:
Original post by Molle85
You include "player.h" first...

forward declare the Mob class

class Player
{
int attack(class Mob& player);
}


thank you!!!!!!!!!!!! i love you.

jk.

but seriously, ty.

Share this post


Link to post
Share on other sites

This topic is 3464 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this