C++ 读取结构时发生未处理的异常
程序在此行抛出未处理的异常:C++ 读取结构时发生未处理的异常,c++,exception,structure,unhandled,C++,Exception,Structure,Unhandled,程序在此行抛出未处理的异常: }else if(s == "backpack"){ cout << "\nEquipped items: " << endl; cout << weapon->Name << endl << cArmour->Name << endl; //this line 在播放器构造函数中,它创建结构对象: Player::Player(std::map<std::s
}else if(s == "backpack"){
cout << "\nEquipped items: " << endl;
cout << weapon->Name << endl << cArmour->Name << endl; //this line
在播放器构造函数中,它创建结构对象:
Player::Player(std::map<std::string,sWeapon*> wepArray,std::map<std::string,sArmour*> armArray){
weapons = wepArray;
armour = armArray;
weapon = wepArray["None"];
cArmour = armArray["None"];
}
Player::Player(std::map wepArray,std::map armArray){
武器=威帕雷;
装甲=武装;
武器=wepArray[“无”];
cArmour=armArray[“无”];
}
在整个计划开始时,它称为初始武器和初始装甲:
int main(){
using namespace std;
//initialise the game
std::map<std::string,sWeapon*> wepArray = init_weapons(); //get weapon array
std::map<std::string,sArmour*>armArray = init_armour(); //get armour array
intmain(){
使用名称空间std;
//初始化游戏
std::map wepArray=init_武器();//获取武器数组
std::maparmArray=init_armour();//获取armour数组
返回所有武器的地图:
//init_weapons()
//sets up weapons map
std::map<std::string,sWeapon*> init_weapons(void){
std::map< std::string, sWeapon* > weapons; //map of weapons
//starting 'none'
sWeapon* none = new sWeapon();
none->Name = "None";
none->Damage = 0;
//create weapons
sWeapon* w1 = new sWeapon();
w1->Name = "Rusty dagger";
w1->Damage = 3;
//put in map
weapons[w1->Name] = w1;
return weapons;
}
std::map<std::string,sArmour*> init_armour(void){
std::map< std::string, sArmour* > armour; //map of armour
//starting 'none'
sArmour* none = new sArmour();
none->Name = "None";
none->AP = 0;
//create armour
sArmour* a1 = new sArmour();
a1->Name = "Leather";
a1->AP = 10;
//put in map
armour[a1->Name] = a1;
return armour;
}
//init_武器()
//设置武器地图
地图初始武器(无效){
std::map武器;//武器地图
//开始“无”
sWeapon*无=新sWeapon();
无->名称=“无”;
无->损坏=0;
//制造武器
sWeapon*w1=新sWeapon();
w1->Name=“生锈的匕首”;
w1->损伤=3;
//放入地图
武器[w1->名称]=w1;
归还武器;
}
标准::地图初始装甲(无效){
std::map装甲;//装甲地图
//开始“无”
sArmour*none=新的sArmour();
无->名称=“无”;
无->AP=0;
//制造盔甲
sArmour*a1=新的sArmour();
a1->Name=“皮革”;
a1->AP=10;
//放入地图
装甲[a1->名称]=a1;
返回装甲;
}
然后将这些映射作为参数传递给上面显示的玩家构造函数。我猜
武器或卡莫尔
为空或不指向任何地方
这更有可能,因为你没有将你的“无”武器和盔甲存储在你的全局散列中
尝试打印这两个“无”的指针对象,然后对象对象的指针值<代码>武器<代码>或代码> CARMUM[/COP>.< /P>它抛出什么异常?请粘贴这个问题。如果你想,你可以砍掉那些讨厌的比特,因为C++错误消息通常是巨大的。@第一个评论-访问违反读取位置。解除装甲?猪头:我想再次+1,但我不能:)你将w1
和a1
添加到地图中,但不要将none
添加到任何地图中。那就是问题所在!谢谢
//init_weapons()
//sets up weapons map
std::map<std::string,sWeapon*> init_weapons(void){
std::map< std::string, sWeapon* > weapons; //map of weapons
//starting 'none'
sWeapon* none = new sWeapon();
none->Name = "None";
none->Damage = 0;
//create weapons
sWeapon* w1 = new sWeapon();
w1->Name = "Rusty dagger";
w1->Damage = 3;
//put in map
weapons[w1->Name] = w1;
return weapons;
}
std::map<std::string,sArmour*> init_armour(void){
std::map< std::string, sArmour* > armour; //map of armour
//starting 'none'
sArmour* none = new sArmour();
none->Name = "None";
none->AP = 0;
//create armour
sArmour* a1 = new sArmour();
a1->Name = "Leather";
a1->AP = 10;
//put in map
armour[a1->Name] = a1;
return armour;
}