Jump to content
  • Advertisement
Sign in to follow this  
VISQI

please help me with this

This topic is 3013 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

okay, i know that the errors for this code might seem easy but i swear i can't find it so either my two compilers went mad or i did
please take a look at it(mainly at the Potion class) and give me ur opinion for the structure of it too
it should be a simple text-based RPG game
here is the code:
#include<iostream>
#include<string>
#include<ctime>
#include<vector>
#include<cstdlib>


using namespace std;

class Player
{
public:
Player(const string& name = "Player", int health = 100, bool ispowered = false, bool isenchated = false):m_name(name), m_health(health), m_ispowered(false), m_isenchanted(false)
{}
virtual ~Player();//clear the potion and weapon vector and pointers
int gethealth() const{return m_health;}
void sethealth(int health){m_health = health;}
string getname() const{return m_name;}
bool getispowered() const {return m_ispowered;}
void powerup() {if(!m_ispowered) m_ispowered = true;}
void powerdown() {if(m_ispowered) m_ispowered = false;}
int weaponattack() {return m_weapon-&gt;attack();}
void enchantup();
void potionmenu();
void chooseweapon();
int menu();//let it act out those choices too not just return them
void addpotion(Potion* p);
private:
string m_name;
int m_health;
vector&lt;Potion*&gt; m_potions;
bool m_ispowered;
bool m_isenchanted;
Weapon* m_weapon;
};


Player::~Player()
{
for(vector&lt;Potion*&gt;::iterator iter = m_potions.begin(); iter!= m_potions.end(); iter++)
{
delete *iter;
*iter = 0;
}
m_potions.clear();

delete m_weapon;
m_weapon = 0;
}

void Player::addpotion(Potion* p)
{
m_potions.push_back(p);
}

void Player::potionmenu()
{
int health = 0;
int mana = 0;
int power = 0;
int enchant = 0;

for(vector&lt;Potion*&gt;::iterator iter = m_potions.begin(); iter != m_potions.end(); iter++)
{
if((*iter)-&gt;getname() == "Health Potion" && (*iter)-&gt;getused() == false)
health++;
else if((*iter)-&gt;getname() == "Mana Potion" && (*iter)-&gt;getused() == false)
mana++;
else if((*iter)-&gt;getname() == "Power Potion" && (*iter)-&gt;getused() == false)
power++;
else if((*iter)-&gt;getname() == "Enchantment Potion" && (*iter)-&gt;getused() == false)
enchant++;
}
cout &lt;&lt; "1- Health Potion (" &lt;&lt; health &lt;&lt; " left)" &lt;&lt; endl
&lt;&lt; "2- Mana Potion (" &lt;&lt; Mana &lt;&lt; " left)" &lt;&lt; endl
&lt;&lt; "3- Power Potion (" &lt;&lt; power &lt;&lt; " left)" &lt;&lt; endl
&lt;&lt; "4- Enchantment Potion (" &lt;&lt; enchant &lt;&lt; " left)" &lt;&lt; endl
&lt;&lt; "5- Exit to battle system\n";
int choice;
cin &gt;&gt; choice;
switch(choice)
{
case 1:
{

if(health != 0)
{
m_health = m_health + 40;
for(vector&lt;Potion*&gt;::iterator iter = m_potions.begin(); iter != m_potions.end(); iter++)
if((*iter)-&gt;getname() == "Health potion")
(*iter)-&gt;unuse();
}
else
cout &lt;&lt; "No Health potions available\n";
break;
}

case 2:
{
if(mana != 0)
{
m_weapon-&gt;manapotion();
for(vector&lt;Potion*&gt;::iterator iter = m_potions.begin(); iter != m_potions.end(); iter++)
if((*iter)-&gt;getname() == "Mana potion")
(*iter)-&gt;unuse();
}
else
cout &lt;&lt; "No Mana potions available\n";
break;
}
case 3:
{

if(power != 0 && m_ispowered == false)
{
powerup();
for(vector&lt;Potion*&gt;::iterator iter = m_potions.begin(); iter != m_potions.end(); iter++)
if((*iter)-&gt;getname() == "Power potion")
(*iter)-&gt;unuse();
}
else if(m_ispowered == true)
cout &lt;&lt; "Already powered up\n";
else
cout &lt;&lt; "No Power potions available\n";
break;
}
case 4:
{
if(enchant != 0 && m_isenchanted == false)
{
enchantup();
for(vector&lt;Potion*&gt;::iterator iter = m_potions.begin(); iter != m_potions.end(); iter++)
if((*iter)-&gt;getname() == "Enchantment potion")
(*iter)-&gt;unuse();
}
else if(m_isenchanted == true)
cout &lt;&lt; "Already enchanted\n";
else
cout &lt;&lt; "No Enchantment potions left\n";
break;
}
case 5:
break;
}

}

void Player::chooseweapon()
{
int choice;
cout &lt;&lt; "Which weapon to choose, stranger\n\n"
&lt;&lt;"1- Broadsword\n"
&lt;&lt;"2- Long Bow\n"
&lt;&lt;"3- Mage Staff\n";
cin &gt;&gt; choice;
switch(choice)
{
case 1:m_weapon = new Sword; break;
case 2:m_weapon = new Bow; break;
case 3:m_weapon = new Staff; break;
}
}

int Player::menu()
{
int choice;
cout &lt;&lt; "BATTLE SYSTEM ENGAGED:\n";
do{

cout &lt;&lt; "1- attack the enemy\n";
cout &lt;&lt; "2- use a potion\n";
cout &lt;&lt; "3- skip\n";
cin &gt;&gt; choice;
if(choice &gt; 3 || choice &lt; 1)
cout &lt;&lt; "Choose one of the three options\n";
else
return choice;
}while(choice &gt; 3 || choice &lt; 1);
}

void Player::enchantup()
{
int weapon;
if(m_weapon-&gt;getname() == "Broadsword")
weapon = 1;
else if(m_weapon-&gt;getname() == "Long Bow")
weapon = 2;
else if(m_weapon-&gt;getname() == "Mage Staff")
weapon = 3;

delete m_weapon;
m_weapon = 0;
if(weapon = 1)
m_weapon = new Xcaliber;
else if(weapon = 2)
m_weapon = new Darkdeath;
else if(weapon = 3)
m_weapon = new Slight;
}

class Weapon
{
public:
Weapon(const string& name = " ",int power = NULL, int mana = NULL):m_name(name),m_power(power), m_mana(mana){}
virtual ~Weapon() {}
virtual int attack() = 0;//
int getmana() const {return m_mana;}
string getname() const {return m_name;}
int getpower() const{return m_power;}
void setpower(int power){m_power = power;}
void setname(string name){m_name = name;}
void manapotion(){m_mana = m_mana + 40;}
protected:
int m_mana;
int m_power;
string m_name;
};

class Sword : public Weapon
{
public:
Sword(const string& name = "Broadsword", int power = 40, int mana = NULL):Weapon(name, power, mana)
{}
virtual ~Sword() {};
virtual int attack();
};

int Sword::attack()
{
cout &lt;&lt; "The brave warrior strikes his blade with all his might damaging the enemy with a power of " &lt;&lt; m_power &lt;&lt; endl;
return m_power;
}


class Xcaliber: public Sword
{
public:
Xcaliber(const string& name = "ExCaliber", int power = 80, int mana = -1):Sword(name, power, mana)
{
cout &lt;&lt; "Only few have lived to tell the story of the legendary ExCaliber. It was said that one blow of this sword can annihilate the most feared enemies, may the sword bearer make peace with this fine piece of warcraft\n";
}
virtual ~Xcaliber();
virtual int attack();
};

int Xcaliber::attack()
{
cout &lt;&lt; "The fierce blader strikes down the target with a power of " &lt;&lt; m_power &lt;&lt; endl;
return m_power;
}

//////////////////////////////////////////////////////////////////

class Bow : public Weapon
{
public:
Bow(const string& name = "Long Bow", int power = NULL, int mana = NULL):Weapon(name, power, mana)
{}
virtual ~Bow(){}
int hit();//random num generator from 0 to 50
virtual int attack();
};

int Bow::attack()
{
int random = hit();
if(random == 0)
{
m_power = 0;
cout &lt;&lt; "Miss!\n";
}
else if (random &gt; 0 && random &lt;= 20)
{
m_power = 20;
cout &lt;&lt; "the mighty " &lt;&lt; m_name &lt;&lt; " hit the target with a power of " &lt;&lt; m_power &lt;&lt; endl;
}
else if (random &gt; 20 && random &lt;= 40)
{
m_power = 40;
cout &lt;&lt; "the mighty " &lt;&lt; m_name &lt;&lt; " hit the target with a power of " &lt;&lt; m_power &lt;&lt; endl;
}
else if (random &gt; 40 && random &lt;= 45)
{
m_power = 50;
cout &lt;&lt; "The Fearful master struck the target with a near death kill with a power of " &lt;&lt; m_power &lt;&lt; endl;
}
else if (random &gt; 45 && random &lt;= 50)
{
m_power = 70;
cout &lt;&lt; "the Death Merchant have given the enemy a point blank shot to the heart with a power of " &lt;&lt; m_power &lt;&lt; endl;
}

return m_power;
}


class Darkdeath :public Bow
{
public:
Darkdeath(const string& name = "Dark Death", int power = NULL, int mana = NULL): Bow(name, power, mana)
{
cout &lt;&lt; "The darkness have surrounded the bow bearer and have created the ultimate weapon in long-range archery, Dark Death\n";
}
virtual int attack();
};

int Darkdeath::attack()
{
int random = hit();
if(random &gt;= 0 && random &lt; 40)
{
m_power = 80;
cout &lt;&lt; "Dark Death has struck down the enemy with a power of " &lt;&lt; m_power &lt;&lt; endl;
}
else if(random &gt;= 40 && random &lt; 45)
{
m_power = 90;
cout &lt;&lt; "Dark Death has struck down the enemy with a power of " &lt;&lt; m_power &lt;&lt; endl;
}
else if(random &gt;= 45 && random &lt;= 50)
{
m_power = 100;
cout &lt;&lt; "\"MAY THERE BE DARKNESS!!\" the Dark Death bearer said while striking the enemy with a point blank shot to the core with a power of " &lt;&lt; m_power &lt;&lt; endl;
}
return m_power;
}

/////////////////////////////////////////////////////////////

class Staff : public Weapon
{
public:
Staff(const string& name = "Mage Staff", int power = NULL, int mana = 100):Weapon(name, power, mana)
{}
virtual ~Staff() {}
virtual int attack();
};

int Staff::attack()
{
int choice;
cout &lt;&lt; "do you want to:\n1- cast magic&lt;" &lt;&lt; m_mana &lt;&lt; " left and 20 mana is needed to attack\n";
cout &lt;&lt; "2-attack with the staff(revives 30 mana)\n";
cin &gt;&gt; choice;
if(choice == 1)
{
m_mana = m_mana - 20;
m_power = 60;
}
else if(choice == 2)
{
m_mana = m_mana + 30;
m_power = 20;
}
return m_power;
}

class Slight : public Staff
{
public:
Slight(const string& name = "Staff of Lightning", int power = NULL, int mana = 200) :Staff(name, power, mana)
{
cout &lt;&lt; "The staff have accepted its user for all his might and will and have unvieled its true power, now the staff Bearer shall have lightning by his side each time he strikes with his will\n\n";
}
virtual ~Slight() {};
virtual int attack();
};

int Slight::attack()
{
int choice;
cout &lt;&lt; "do you want to:\n1- cast magic&lt;" &lt;&lt; m_mana &lt;&lt; " left and 10 mana is needed to attack\n";
cout &lt;&lt; "2-attack with the staff(revives 40 mana)\n";
cin &gt;&gt; choice;
if(choice == 1)
{
m_mana = m_mana - 10;
m_power = 120;
}
else if(choice == 2)
{
m_mana = m_mana + 40;
m_power = 60;
}
return m_power;
}

class Potion
{
public:
Potion(const string& name = "potion", bool used = false);
~Potion();
void unuse() {m_used = !m_used;}
string getname() const{return m_name;}
bool getused() const{return m_used;}
private:
string m_name;
bool m_used;

};

Potion::Potion(const string& name, bool used)
{
m_name = name;
m_used = false;
cout &lt;&lt; "A new Potion was aquired: " &lt;&lt; m_name &lt;&lt; "\n\n";
}

Potion::~Potion()
{}

class Enemy
{
public:
Enemy(const string& name = "Enemy", int power = 40, int health = 100, bool dead = false):m_name(name), m_power(power), m_health(health), m_dead(dead)
{}
virtual ~Enemy(){}
string getname() const{return m_name;}
int gethealth() const{return m_health;}
void sethealth(int health){m_health = health;}
int random();
void dead(){cout &lt;&lt; "The Enemy is dead\n"; if(!m_dead) m_dead = true;}
void drop(Player& p);
void turn(Player& p)
{
if(!m_dead)
{
cout &lt;&lt; "the enemy is about to attack\n";
p.sethealth(p.gethealth() = p.gethealth() - m_power);
cout &lt;&lt; "the enemy has slashed " &lt;&lt; m_power &lt;&lt; " health points from your life\n";
}
else
cout &lt;&lt; "No More Enemies left\n\n";
}

private:
string m_name;
int m_power;
int m_health;
bool m_dead;
}
void Enemy::drop(Player& p)
{
if(random() &gt;= 50)
{
cout &lt;&lt; "you got a drop\n";
int a[3] = {1, 2, 3};
srand(time(0));
int random = (rand() % 2) + 0;
if(a[random] == 1)
p.addpotion(new Potion("Health Potion"));
else if(a[random] == 2)
p.addpotion(new Potion("Mana Potion"));
else if(a[random] == 3)
p.addpotion(new Potion("Power Potion"));
}
}

class Boss : public Enemy
{
public:
Boss(const string& name = "Troll", int power = 80, int health = 150, bool dead = false):Enemy(name, power, health, dead)
{
cout &lt;&lt; "BEWARE OF THE DEADLY TROLL\n\n";
}
virtual ~Boss(){}
};

class Map
{
public:
Map(const string& name = "Map"): m_name(name){}
virtual ~Map() {}
virtual void play() = 0;
string getname() const{return m_name;}
virtual int enemypack() = 0;//random number generator
void invasion();
void bossinvasion();

protected:
string m_name;
Player m_player;
};

void Map::invasion()
{
vector&lt;Enemy*&gt; wolfpack;
int random = enemypack();
wolfpack.reserve(random);
for(int i = 0; i &lt; random; i++)
wolfpack.push_back(new Enemy);

int i = 0;
for(vector&lt;Enemy*&gt;::const_iterator iter = wolfpack.begin(); iter!= wolfpack.end(); iter++)
i++;

cout &lt;&lt; "INVASION ALERT!!\nINITIATING BATTLE SYSTEM\n\n";

for(vector&lt;Enemy*&gt;::const_iterator iter = wolfpack.begin(); iter != wolfpack.end(); iter++)
{

while(m_player.gethealth() &gt; 0 && (*iter)-&gt;gethealth() &gt; 0)
{

if(m_player.gethealth() &lt;= 0)
{
cout &lt;&lt; "YOU ARE DEAD, DEAD, DEAD\n\n";
exit(0);
}
cout &lt;&lt; i &lt;&lt; "enemies left\n";
switch(m_player.menu())
{
case 1:
{
int enemyhealth = (*iter)-&gt;gethealth();
enemyhealth = enemyhealth - m_player.Weaponattack();
(*iter)-&gt;sethealth(enemyhealth);
if(enemyhealth &lt;= 0)
{
(*iter)-&gt;dead();
(*iter)-&gt;drop(m_player);
}
break;
}

case 2:
{
cout &lt;&lt; "the available potions are:\n\n";
m_player.potionmenu();
break;
}
case 3:
cout &lt;&lt; "you have skipped your turn\n\n";
}

(*iter)-&gt;turn(m_player);//make it 70% chance to hit the player and activate the attack() function in it
}

}

for(vector&lt;Enemy*&gt;::iterator iter = wolfpack.begin(); iter != wolfpack.end(); iter++)
{
delete *iter;
(*iter) = 0;
}
wolfpack.clear();
}

void Map::bossinvasion()
{
Enemy* boss = new Boss;
cout &lt;&lt; "INVASION ALERT!!\nINITIATING BATTLE SYSTEM\n\n";
while(m_player.gethealth() &gt; 0 && boss-&gt;gethealth() &gt; 0)
{

if(m_player.gethealth() &lt;= 0)
{
cout &lt;&lt; "YOU ARE DEAD, DEAD, DEAD\n\n";
exit(0);
}
switch(m_player.menu())
{
case 1:
{
int enemyhealth = boss-&gt;gethealth();
enemyhealth = enemyhealth - m_player.Weaponattack();
boss-&gt;sethealth(enemyhealth);
if(enemyhealth &lt;= 0)
{
boss-&gt;dead();
boss-&gt;drop(m_player);
}
break;
}

case 2:
{
cout &lt;&lt; "the available potions are:\n\n";
m_player.potionmenu();
break;
}
case 3:
cout &lt;&lt; "you have skipped your turn\n\n";
}

boss-&gt;turn(m_player);//make it 70% chance to hit the player and activate the attack() function in it
}

delete boss;
boss = 0;
}


class City : public Map
{
public:
City(const string& name = "City Map"):Map(name)
{}
virtual ~City() {}
virtual void play();
virtual int enemypack();
};

int City::enemypack()
{
int a[3] = {1, 2, 3};
srand(time(0));
int random = (rand() % 2) + 0;
return a[random];
}


void City::play()
{
m_player.chooseweapon();
cout &lt;&lt; "The Brave warrior enters the infested city\n\n";
invasion();
cout &lt;&lt; "Luckily he was able to help the city before it got nasty\n\n";
cout &lt;&lt; "As a Token of the city's appreciation, they gave you the potion of power\n"
&lt;&lt; "Legend has it that the only way to defeat the evil troll is to find the magical potion of enchantment in the woods\n";

}

class Forest : public Map
{
public:
Forest(const string& name = "Forest Map"):Map(name)
{}
virtual ~Forest();
virtual void play();
virtual int enemypack();
};

int Forest::enemypack()
{
int a[3] = {4, 5, 6};
srand(time(0));
int random = (rand() % 2) + 0;
return a[random];
}

void Forest::play()
{
cout &lt;&lt; "The brave warrior enters the forbidden forest\n\n";
invasion();
cout &lt;&lt; "The damned forest shows another one of its tricks, the fearful troll\n";
bossinvasion();

Potion* enchantment = new Potion("Enchantment Potion");
m_player.addpotion(enchantment);
cout &lt;&lt; "\n\nAfter a fierce battle, the warrior was able to aquire the potion of enchantment(you can use it next time in the potions menu in the battle system)\n\n";

}

class Duengon : public Map
{
public:
Duengon(const string & name = "Duengon Map"):Map(name)
{}
virtual ~Duengon() {}
virtual void play();
virtual int enemypack();
};

int Duengon::enemypack()
{
int a[3] = {5, 6, 7};
srand(time(0));
int random = (rand() % 2) + 0;
return a[random];
}


void Duengon::play()
{
cout &lt;&lt; "the brave warrior enters the duengon\n\n";
invasion();
cout &lt;&lt; "After all those battles the warrior finally was able to find the dragon to slay him once and for all\n\n";
bossinvasion();
cout &lt;&lt; "The dragon was slain, ending his reign of terror\n\n";
}

class RPG
{
public:
RPG(){addmap;}
~RPG();
void addmap();
void start();
private:
vector&lt;Map*&gt; m_maps;
};

RPG::~RPG()
{
for(vector&lt;Map*&gt;::iterator iter = m_maps.begin(); iter != m_maps.end(); iter++)
{
delete *iter;
*iter = 0;
}
m_maps.clear();
}
void RPG::addmap()
{
m_maps.push_back(new City);
m_maps.push_back(new Forest);
m_maps.push_back(new Duengon);
}

void RPG::start()
{
for(vector&lt;Map*&gt;::iterator iter = m_maps.begin(); iter != m_maps.end(); iter++)
(*iter)-&gt;play();
}

int main()
{
cout &lt;&lt; "\t\tWELCOME\n\n\n";
RPG game;
game.start();

return 0;
}


[Edited by - VISQI on July 18, 2010 5:58:16 AM]

Share this post


Link to post
Share on other sites
Advertisement
Geez

Repost between [source] and [/source] tags, and at at least tell us what the compiler says.

People can still surprise me.

Share this post


Link to post
Share on other sites
You forgot semicolon (;) after closing bracket for Enemy class. Same with few other classes.

Share this post


Link to post
Share on other sites
okay, the main errors are consistent with the Potion class like:

-error c2061: syntax error: identifier 'Potion' (line 27)

-error c2065: 'Potion':undeclared identifier(line 31)

-IntelliSense: no suitable user-defined conversion from "std::_Vector_iterator<std::_Vector_val<<error-type> *, std::allocator<<error-type> *>>>" to "std::_Vector_iterator<std::_Vector_val<Potion *, std::allocator<Potion *>>>" exists(line 40)

so basically everything is related to either Potion class or the m_potions vector declared in Player class

and there is some problems with the m_weapon pointer in the Player class,
here is the error:
-intellisense: expression must have pointer-to-class type (line 22)

m_weapon is currently pointed at NULL but in the play() function in City class, i invoke the chooseweapon() function so it will make m_weapon point to something(Sword, Bow, or Staff objects on the heap)

that is mostly the problem

Share this post


Link to post
Share on other sites
You need a forward declaration for Potion and Weapon since Player doesnt know what a Potion/Player is; BTW this is why us use header's, as painful as they are, especially for small utility classes, they tend to make your life a little easier.

Share this post


Link to post
Share on other sites
what do u mean by a forward declaration??? do u mean i should make Weapon and Potion classes a friend classes in Player???
and what do you think of the structure of the code itself and is there anyway to make it better??
appreciated

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!