我想製作一個「英雄」對象的對象向量。C++中對象的多態數組更改對象的詳細信息
這是我發表的聲明:
vector<hero*> arr;
,這些都是在進入對象插入到陣列(射手是從英雄對象inhert)
archer a('a'+to_string(numPlayer),gend,hit, point, point2);
this->arr.insert (this->arr.begin() + numPlayer, &a);
這似乎是線一切都在工作,但是當我試圖獲得對象的健康時,它顯示我6但它應該是100.爲什麼? 我也嘗試獲取對象的名稱,但它給我分段錯誤。爲什麼它會發生?
這是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
,這些都是弓箭手和英雄建設者
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) {
}
這是GET:cout<<this->arr.at(0)->getHealth();
//輸出6
cout<<this->arr.at(0)->getName();
//輸出:擊:第12行:23084段錯誤$ file.o的$ args
解決此類問題的正確工具是您的調試器。在*堆棧溢出問題之前,您應該逐行執行您的代碼。如需更多幫助,請閱讀[如何調試小程序(由Eric Lippert撰寫)](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/)。至少,您應該\編輯您的問題,以包含一個[最小,完整和可驗證](http://stackoverflow.com/help/mcve)示例,該示例再現了您的問題,以及您在調試器。 –
'&a'?有些東西告訴我,當你實際上從存儲在該向量中的指針開始執行檢查和/或名稱打印時,由於退出創建範圍而被銷燬的'a'具有* long *。總之,我的水晶球告訴我你正在加載你的矢量,最終變成一個懸掛指針。 – WhozCraig
順便說一句,戰士,弓箭手和嚮導類沒有複製構造函數(缺少const關鍵字)。 –