I searched the threads for help, but I'm not sure my terminology is correct. I'm a C++ hobbyist, and have a question about maintaining a list of objects, created dynamically at run time, and how to interact with those objects. I'd like to maybe use a vector, and some pointers, but I can't get it together. This is my first post.
I've gotten it to work without the vector and list ideas where it just used 4 statically created class instances. I know it's a lot of code, but basically - all I want to do is maintain a vector of pointers (I think) to Monster objects created with new. I think. Any help is awesome, even a link to something or anything. I'm studying Accelerated C++ (Koenig, Moo) but it's complicated for me - the going is slow.
Here's the class:
Code:
#ifndef MONSTER_H
#define MONSTER_H
#include <string>
using namespace std;
extern int monster_number;
class Monster
{
public:
Monster(string n = "Unknown", int hp = 500, int ap = 1000, int spd = 1000);
virtual ~Monster();
void report();
string myname();
private:
string name;
int max_health;
int cur_health;
int atk_pwr;
int speed;
};
#endif // MONSTER_H
Here's the class implementation:
Code:
#include "Monster.h"
#include <iostream>
using namespace std;
Monster::Monster(string n, int hp, int ap, int spd)
{
monster_number++;
cout << "Monster #" << monster_number << " Born!" << endl;
name = n;
max_health = hp;
cur_health = hp;
atk_pwr = ap;
speed = spd;
monster_pool.push_back(this); // This fails horribly
}
Monster::~Monster()
{
cout << "Monster #" << monster_number << " Killed!" << endl;
monster_number--;
}
void Monster::report()
{
cout << endl;
cout << "Name: " << name << endl;
cout << " Hp: " << cur_health << "/" << max_health << endl;
cout << " Attack Power: " << atk_pwr << " Speed: " << speed << endl;
}
string Monster::myname()
{
return name;
}
And the main program:
Code:
#include <iostream>
#include <vector>
#include <cctype>
#include "Monster.h"
using namespace std;
int monster_number = 0;
vector<Monster> monster_pool;
int main()
{
//vector<Monster> monster_pool;
Monster Dragon( "Red Dragon", 5500, 450, 130 );
Monster Goblin( "Goblin", 45, 65, 55 );
Monster Orc( "Orc", 95, 135, 75 );
Monster Worg( "Worg", 65, 125, 90 );
//monster_pool.push_back(Goblin);
//monster_pool.push_back(Orc);
//monster_pool.push_back(Worg);
bool Running = true;
char choice = '?';
while( Running )
{
cout << endl;
cout << "Monster Menu" << endl;
cout << "(S)pawn a monster" << endl;
cout << "(K)ill a monster" << endl;
cout << "(L)ist all monsters" << endl;
cout << "(Q)uit" << endl;
cout << "Choice? ";
cin >> choice;
switch( toupper( choice ) )
{
case 'S':
cout << endl;
cout << "Spawn Monster" << endl;
cout << "-------------" << endl;
cout << "(D)ragon" << endl;
cout << "(G)oblin" << endl;
cout << "(O)rc" << endl;
cout << "(W)org" << endl;
cout << "Spawn which type? ";
// cin >> choice;
// Monsters.spawn(choice);
//Monster *m+monster_number;
break;
case 'K':
cout << endl;
cout << "Kill which monster?" << endl;
cout << "-------------------" << endl;
// Monsters.list();
cin >> choice;
// Monsters.kill(choice);
break;
case 'L':
cout << endl;
cout << "Monster List:" << endl;
cout << "-------------" << endl;
// Monsters.list();
break;
case 'Q':
Running = false;
break;
default:
break;
}
}
cout << endl << "Ending with " << monster_number << " monsters in the wild!" << endl;
if( monster_number == 0 ) cout << "You killed all the monsters, hero." << endl;
else cout << "I will slay them..." << endl << endl;
return 0;
}