Creating simple RPG Stats not so simple

Hey all, I’m pretty noob at C++. I’m hoping I have some semblance of a program built though. I basically want to take 2 players, using at least 2 classes; a player class and a weapons and/or armor subclass. I want to show their stats after each battle when they gain exp. The numbers don’t even really need to make much sense. I just need to get out of compile error hell at this point. Can anyone help me figure out how to debug this? I’m getting some compile errors, but have no idea where to start. In my subclasses I am basically making one static weapon and armor and assuming they are both using it.


// MCrowley.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

class Player
{
public:
    Player();
    Player(string, int, int, int, int, int);
    void setPlayer(string, int, int, int, int, int);
    string getPlayer(int& lvl, int& exp, int& hp, int& str, int& def);
    void printPlayer();
    Player operator+(const Player&) const;    
private:
    string playerName;
    int level, experiencePoints, hitPoints, strength, defence;
};

class PlayerArmor: public Player //child (inherited) class
{
public:
    PlayerArmor();
    PlayerArmor(string, int, int, int, int, int, string);
    void setStatusDefence(string);
    void printPlayer() const; //overries parent class print function
private:
    string armorName, statusDefence;
};

class PlayerWeapon: public Player //child (inherited) class
{
public:
    PlayerWeapon();
    PlayerWeapon(string, int, int, int, int, int string);
    void setStatusInflict(string);
    void printPlayer() const; //overries parent class print function
private:
    string weaponName, statusInflict;
};

void Player::setPlayer(string name, int lvl, int exp, int hp, int str, int def) //set data for player
{
    playerName = name;
    level = lvl;
    experiencePoints = exp;
    hitPoints = hp;
    strength = str;
    defence = def;
}

Player::Player()  //default constructor
{
    setPlayer(" ", 1, 0, 100, 1, 1);
}

Player::Player(string name, int lvl, int exp, int hp, int str, int def)  //overloaded constructor
{
    setPlayer(name, lvl, exp, hp, str, def);
}

string Player::getPlayer(int& lvl, int& exp, int& hp, int& str, int& def)  
{
    lvl = level;
    exp = experiencePoints;
    hp = hitPoints;
    str = strength;
    def = defence;
    return playerName;
}

void Player::printPlayer()  
{
string name;
int lvl, exp, hp, str, def;
    name = getPlayer(lvl, exp, hp, str, def);
    cout << name << ":  " << "lvl:  " << lvl << "  " << "exp: "<< exp << "  " << "hp: "<< hp << "  " << "str: "<< str << "  " << "def: "<< def << endl;
}

Player Player::operator+(const Player& otherPL) const
{
Player tempPL;  

    tempPL.playerName = playerName;
    tempPL.experiencePoints = experiencePoints + otherPL.experiencePoints;  
    if (tempPL.experiencePoints > 1000 * level)
    {
        tempPL.level++;
    }
    if (tempPL.level > level)                            
    {
        tempPL.hitPoints = tempPL.hitPoints * 2;    
        tempPL.strength = tempPL.strength * 2;
        tempPL.defence = tempPL.defence * 2;
    }
    return tempPL;  // return object containing totals
}

//Player::~Player()
//{
//}

PlayerArmor::PlayerArmor()
{
    armorName = "Burning Armor";
    statusDefence = "Fire Immune";
}

PlayerArmor::PlayerArmor(string armorName, int lvl, int exp, int hp, int str, int def, string StatusDefence):Player(lvl, exp, hp, str, def)
{
    setStatusDefence(statDef);
    hp = hp + 100;
    def = def + 5;
}

void PlayerArmor::setStatusDefence(string statDef)
{
    statusDefence = statDef;
}

void PlayerArmor::printPlayer()
{
    Player::printPlayer();
    cout<< "Armor Name:  " << armorName; << endl;
    cout<< "Status Defence:  " << statusDefence; << endl;
}

//PlayerArmor::~PlayerArmor()
//{
//}

PlayerWeapon::PlayerWeapon()
{
    weaponName = "Frigid Blade";
    statusInflict = "Blizzaga";
}

PlayerWeapon::PlayerWeapon(string armorName, int str, string statusInflict)
{
    setStatusInflict(statInf);
    str = str + 10;
}

void PlayerWeapon::setStatusInflict(string statInf)
{
    statusInflict = statInf;
}

void PlayerWeapon::printPlayer()
{
    Player::printPlayer();
    cout<< "Weapon Name:  " << weaponName << endl;
    cout<< "Status Inflict:  " << statusInflict << endl;
}

//PlayerWeapon::~PlayerWeapon()
//{
//}

void main()
{
string stats1, stats2;
int totalLevel1, totalLevel2, totalExperiencePoints1, totalExperiencePoints2, totalHitPoints1, totalHitPoints2, totalStrength1, totalStrength2, totalDefence1, totalDefence2;

    Player battle1("Zidane", 1, 900, 100, 1, 1);  //set battle stats
    Player battle2("Zidane", 1, 100, 100, 1, 1);
    Player battle3("Zidane", 2, 1100, 200, 2, 2);
    Player battle4("Vaan", 2, 200, 200, 2, 2);
    Player battle5("Vaan", 2, 400, 200, 2, 2);
    Player battle6("Vaan", 2, 700, 200, 2, 2);

    battle1.printPlayer();   //print out battle stats
    battle2.printPlayer();
    battle3.printPlayer();
    battle4.printPlayer();
    battle5.printPlayer();
    battle6.printPlayer();

    cout << endl << endl << "Totals:" << endl;

    Player zidaneStats = battle1 + battle2 + battle3;   
    zidaneStats.printPlayer();                        // print totals

    Player vaanStats = battle4 + battle5 + battle6;  
    vaanStats.printPlayer();                    // print totals

    stats1 = zidaneStats.getPlayer(totalLevel1, totalExperiencePoints1, totalHitPoints1, totalStrength1, totalDefence1);  //get total battle data for each fighter
    stats2 = vaanStats.getPlayer(totalLevel2, totalExperiencePoints2, totalHitPoints2, totalStrength2, totalDefence2);

    if(totalLevel1 > totalLevel2)                // determine and print the fighter with the highest total level
        cout << endl << endl << stats1 << " gained the highest total lvl." << endl;
    else if(totalLevel1 < totalLevel2)
        cout << stats2 << " gained the highest total lvl." << endl;
    else
        cout << stats1 << " and " << stats2 << " gained the same total levels" << endl;

    if(totalExperiencePoints1 < totalExperiencePoints2)                // determine and print the fighter with the lowest hit points(HP)
        cout << endl << endl << stats1 << " gained the lowest experience points." << endl;
    else if(totalExperiencePoints1 > totalExperiencePoints2)
        cout << stats2 << " gained the lowest experience points." << endl;
    else
        cout << stats1 << " and " << stats2 << " gained the same experience points" << endl;

    cin.ignore(2);
}

Thanks in advance!

Ok, down to 5 errors. Problems with overloading… Any ideas?

Can anyone give me a hand with this if you got a sec. I know I am implementing one or more things incorrectly in functions. Problem is I don’t have a strong enough background to figure out how to get out of it.

Anyone? I’m trying to improve my skills with C++, but I am so used to Actionscript that this is a totally different monster syntax-wise.

One sec I’ll stick the code in my compiler as I cba to go through it line by line :smiley:

Ok you have an error in your PlayerArmour constructor - it looks like you’ve used Player as a base by copying the function header and by accident have left the signature of the Player constructor on the end


PlayerArmor::PlayerArmor(string armorName, int lvl, int exp, int hp, int str, int def, string StatusDefence):Player(lvl, exp, hp, str, def)
{
    setStatusDefence(statDef);
    hp = hp + 100;
    def = def + 5;
}
 
 

Also you’ve defined StatusDefence as a string but tried to use ‘statDef’

Now you should get a couple more errors - you have got a member function called ‘printPlayer’ - but the declaration of printPlayer in the PlayerArmour and PlayerWeapon class is

void printPlayer() const;'

whereas it’s

void printPlayer(); 

in the Player class - the signature is different so therefore the compiler is searching for an overload when you call Parent::printPlayer();

Make sure these three are the same or you at least declare your overload which matches the signature in Player

Another thing is the cout << bits, you can’t just do cout << “string”; << “something else”

You need to make sure you call cout for every piece of information unless you concatenate the strings, so…

cout << “string1”; cout << var1; cout << “string2”;

Last and not least you’ve got a dodgy variable name again and you haven’t declared the overload for this particular PlayerWeapon constructor


PlayerWeapon::PlayerWeapon(string armorName, int str, string statusInflict)
{
    setStatusInflict(statInf);
    str = str + 10;
}

Check your statusInflict var - not the same in the function header and the function call in the body, and make sure you declare the overload for this constructor in the class definition

Here’s the fixed code


#include "stdafx.h"
#include <iostream>
#include <string>
#include <iomanip>
 
using namespace std;
 
class Player
{
public:
    Player();
    Player(string, int, int, int, int, int);
    void setPlayer(string, int, int, int, int, int);
    string getPlayer(int& lvl, int& exp, int& hp, int& str, int& def);
    void printPlayer();
    Player operator+(const Player&) const;    
private:
    string playerName;
    int level, experiencePoints, hitPoints, strength, defence;
};
 
class PlayerArmor: public Player //child (inherited) class
{
public:
    PlayerArmor();
    PlayerArmor(string, int, int, int, int, int, string);
    void setStatusDefence(string);
    void printPlayer(); //overries parent class print function
private:
    string armorName, statusDefence;
};
 
class PlayerWeapon: public Player //child (inherited) class
{
public:
    PlayerWeapon();
    PlayerWeapon(string, int, int, int, int, int string);
    PlayerWeapon(string armorName, int str, string statusInflict);
    void setStatusInflict(string);
    void printPlayer(); //overries parent class print function
private:
    string weaponName, statusInflict;
};
 
void Player::setPlayer(string name, int lvl, int exp, int hp, int str, int def) //set data for player
{
    playerName = name;
    level = lvl;
    experiencePoints = exp;
    hitPoints = hp;
    strength = str;
    defence = def;
}
 
Player::Player()  //default constructor
{
    setPlayer(" ", 1, 0, 100, 1, 1);
}
 
Player::Player(string name, int lvl, int exp, int hp, int str, int def)  //overloaded constructor
{
    setPlayer(name, lvl, exp, hp, str, def);
}
 
string Player::getPlayer(int& lvl, int& exp, int& hp, int& str, int& def)  
{
    lvl = level;
    exp = experiencePoints;
    hp = hitPoints;
    str = strength;
    def = defence;
    return playerName;
}
 
void Player::printPlayer()  
{
string name;
int lvl, exp, hp, str, def;
    name = getPlayer(lvl, exp, hp, str, def);
    cout << name << ":  " << "lvl:  " << lvl << "  " << "exp: "<< exp << "  " << "hp: "<< hp << "  " << "str: "<< str << "  " << "def: "<< def << endl;
}
 
Player Player::operator+(const Player& otherPL) const
{
Player tempPL;  
 
    tempPL.playerName = playerName;
    tempPL.experiencePoints = experiencePoints + otherPL.experiencePoints;  
    if (tempPL.experiencePoints > 1000 * level)
    {
        tempPL.level++;
    }
    if (tempPL.level > level)                            
    {
        tempPL.hitPoints = tempPL.hitPoints * 2;    
        tempPL.strength = tempPL.strength * 2;
        tempPL.defence = tempPL.defence * 2;
    }
    return tempPL;  // return object containing totals
}
 
//Player::~Player()
//{
//}
 
PlayerArmor::PlayerArmor()
{
    armorName = "Burning Armor";
    statusDefence = "Fire Immune";
}
 
PlayerArmor::PlayerArmor(string armorName, int lvl, int exp, int hp, int str, int def, string StatusDefence)
{
    setStatusDefence(StatusDefence);
    hp = hp + 100;
    def = def + 5;
}
 
void PlayerArmor::setStatusDefence(string statDef)
{
    statusDefence = statDef;
}
 
void PlayerArmor::printPlayer()
{
    Player::printPlayer();
    cout << "Armor Name:  ";
    cout << armorName; 
    cout << endl;
    cout << "Status Defence:  "; 
    cout << statusDefence; 
    cout << endl;
}
 
//PlayerArmor::~PlayerArmor()
//{
//}
 
PlayerWeapon::PlayerWeapon()
{
    weaponName = "Frigid Blade";
    statusInflict = "Blizzaga";
}
 
PlayerWeapon::PlayerWeapon(string armorName, int str, string statusInflict)
{
    setStatusInflict(statusInflict);
    str = str + 10;
}
 
void PlayerWeapon::setStatusInflict(string statInf)
{
    statusInflict = statInf;
}
 
void PlayerWeapon::printPlayer()
{
    Player::printPlayer();
    cout<< "Weapon Name:  " << weaponName << endl;
    cout<< "Status Inflict:  " << statusInflict << endl;
}
 
//PlayerWeapon::~PlayerWeapon()
//{
//}
 
void main()
{
string stats1, stats2;
int totalLevel1, totalLevel2, totalExperiencePoints1, totalExperiencePoints2, totalHitPoints1, totalHitPoints2, totalStrength1, totalStrength2, totalDefence1, totalDefence2;
 
    Player battle1("Zidane", 1, 900, 100, 1, 1);  //set battle stats
    Player battle2("Zidane", 1, 100, 100, 1, 1);
    Player battle3("Zidane", 2, 1100, 200, 2, 2);
    Player battle4("Vaan", 2, 200, 200, 2, 2);
    Player battle5("Vaan", 2, 400, 200, 2, 2);
    Player battle6("Vaan", 2, 700, 200, 2, 2);
 
    battle1.printPlayer();   //print out battle stats
    battle2.printPlayer();
    battle3.printPlayer();
    battle4.printPlayer();
    battle5.printPlayer();
    battle6.printPlayer();
 
    cout << endl << endl << "Totals:" << endl;
 
    Player zidaneStats = battle1 + battle2 + battle3;   
    zidaneStats.printPlayer();                        // print totals
 
    Player vaanStats = battle4 + battle5 + battle6;  
    vaanStats.printPlayer();                    // print totals
 
    stats1 = zidaneStats.getPlayer(totalLevel1, totalExperiencePoints1, totalHitPoints1, totalStrength1, totalDefence1);  //get total battle data for each fighter
    stats2 = vaanStats.getPlayer(totalLevel2, totalExperiencePoints2, totalHitPoints2, totalStrength2, totalDefence2);
 
    if(totalLevel1 > totalLevel2)                // determine and print the fighter with the highest total level
        cout << endl << endl << stats1 << " gained the highest total lvl." << endl;
    else if(totalLevel1 < totalLevel2)
        cout << stats2 << " gained the highest total lvl." << endl;
    else
        cout << stats1 << " and " << stats2 << " gained the same total levels" << endl;
 
    if(totalExperiencePoints1 < totalExperiencePoints2)                // determine and print the fighter with the lowest hit points(HP)
        cout << endl << endl << stats1 << " gained the lowest experience points." << endl;
    else if(totalExperiencePoints1 > totalExperiencePoints2)
        cout << stats2 << " gained the lowest experience points." << endl;
    else
        cout << stats1 << " and " << stats2 << " gained the same experience points" << endl;
 
    cin.ignore(2);
}

p.s. What are you using to compile? I’m pretty sure Visual C++ Express is completely free now…it’s a great tool!

You are awesome! I really appreciate you taking the time to break it down and explain it to me. I was actually looking more for that than I was for the solution! But I will gladly accept that as well.

I am using Visual Studio 2005 Professional. I got it for a fairly good price through school.

This is my first true C++ class. I have taken C# before and I understand the concepts, but when I get in to trouble, I have no idea how to get out of it. The course I am taking does not offer much for real world applications or even examples that show, here, this is exactly how you do this. It’s all very theoretical and we’re supposed to put together the pieces, sometimes, I feel the pieces even aren’t all there.

I am really eager to learn this as I’m going for a Game and Simulation Program Degree, however I feel that I have not been given the opportunity to learn much in this course. Your explaination probably taught me more than I have learned in the last 2 weeks.

Thanks again!

Cheers,
zant…

No problem mate - I’m no maverick developer and I’ve only really had limited exposure to c++, I’ve written a few games in it but nothing really big.

Once you get so far with your code you can try putting some graphics stuff together, OpenGL is not too hard easy to write for and you could try using the SDL libraries which can help you with a lot of the basics (have a look at http://www.libsdl.org/).

Anyway, if you need any more help let me know!

[quote=Charleh;2340582]No problem mate - I’m no maverick developer and I’ve only really had limited exposure to c++, I’ve written a few games in it but nothing really big.

Once you get so far with your code you can try putting some graphics stuff together, OpenGL is not too hard easy to write for and you could try using the SDL libraries which can help you with a lot of the basics (have a look at http://www.libsdl.org/).

Anyway, if you need any more help let me know![/quote]

Actually I have found C++ with OpenGL quite more fun to do, that’s the other class I am taking: Math for Game Programming II. I haven’t coded my own program yet but we have to figure out the physics of rolling a ball back and forth down a ramp, shooting a projectile out of a cannon, etc and input the correct code to get it to work. I have found it much more fun and challenging. I’m sure pretty soon I’ll be doing more graphic-related stuff with C++. I don’t know when, I know we will be using the Torque engine pretty soon here for making games. Again thanks!

Cheers,
zant…