I would like to make a vector of objects of "hero" object.
this is the declaration I made:
vector<hero*> arr;
and these are the lines entering the object into the array(archer is an object inhert from hero)
archer a('a'+to_string(numPlayer),gend,hit, point, point2);
this->arr.insert (this->arr.begin() + numPlayer, &a);
It seems like everything is working, but when I try to get the health of the object, it show me 6 but it should be 100. why is that? I also try to get the name of the object but it gives me segmentation fault. why does it happend?
this is hero.h:
#ifndef HERO_H
#define HERO_H
#include <string>
#include <math.h>
#include <map>
#include <iostream>
#include "point2d.hpp"
//#include "enemy.hpp"
using namespace std;
#include "actors.hpp"
class hero:public actors{
protected:
string name;
int size;
char gender;
double health;
double hitting;
point2d point, goal;
double radius;
public:
hero(string name, char gender, double damage, point2d point, point2d goal);
hero(const hero &hero);
~hero();
string getName();
char getGender();
double getDamage();
point2d getPoint();
double getHealth();
void setName(string name);
void setGender(char gender);
void setDamage(double damage);
void setHealth(double health);
void setPoint(point2d point);
string toString(); ///const in the end!!!!
bool move(map<string, char> &matrix, vector<potion> potions );
double getRadius();
void hurt(double hit);
};
class warrior :public hero{
public:
warrior(string name, char gender, double damage, point2d point, point2d point2);
warrior(warrior &w);
~warrior();
};
class archer :public hero{
public:
archer(string name, char gender, double damage, point2d point, point2d point2);
archer(archer &a);
~archer();
};
class wizard :public hero{
public:
wizard(string name, char gender, double damage, point2d point, point2d point2);
wizard(wizard &a);
~wizard();
};
#endif
and these are archer and hero constructors
hero::hero(string name, char gender, double damage, point2d point,point2d point2){
this->name = name;
this->gender=gender;
this->health=100;
this->hitting=damage;
this->point = point2d(point);
this->goal=point2d(point2);
this->size=0;
}
archer::archer(string name, char gender, double damage, point2d point, point2d point2):
hero(name, gender, damage, point, point2) {
}
this is the get: cout<<this->arr.at(0)->getHealth();
// output is 6
cout<<this->arr.at(0)->getName();
//output:bash: line 12: 23084 Segmentation fault $file.o $args