读取结构时出现未处理的异常
程序在此行引发未处理的异常:
}else if(s == "backpack"){
cout << "\nEquipped items: " << endl;
cout << weapon->Name << endl << cArmour->Name << endl; //this line
它打印“装备的物品:”,然后引发异常。此文件 - Player.h - 包括 Library.h,后者又包括 Globals.h,后者具有以下结构:
struct sWeapon{
std::string Name;
int Damage;
};
struct sArmour{
std::string Name;
int AP;
};
在 Player 构造函数中,它创建结构对象:
Player::Player(std::map<std::string,sWeapon*> wepArray,std::map<std::string,sArmour*> armArray){
weapons = wepArray;
armour = armArray;
weapon = wepArray["None"];
cArmour = armArray["None"];
}
在整个程序的开头,它调用 init_weapons 和 init_armour:
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
返回所有武器的地图:
//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;
}
然后将这些地图作为参数传递给上面所示的玩家构造函数。
The program throws an unhandled exception on this line:
}else if(s == "backpack"){
cout << "\nEquipped items: " << endl;
cout << weapon->Name << endl << cArmour->Name << endl; //this line
It prints 'Equipped items: ' then throws an exception. This file - Player.h - includes Library.h, which in turn includes Globals.h, which has the structs:
struct sWeapon{
std::string Name;
int Damage;
};
struct sArmour{
std::string Name;
int AP;
};
In the Player constructor, it creates the struct objects:
Player::Player(std::map<std::string,sWeapon*> wepArray,std::map<std::string,sArmour*> armArray){
weapons = wepArray;
armour = armArray;
weapon = wepArray["None"];
cArmour = armArray["None"];
}
At the beginning of the entire program, it calls init_weapons and init_armour:
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
which return the map of all the weapons:
//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;
}
then passes those maps as arguments to the player constructor shown above.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我猜测
weapon
或cArmour
为 null 或无处可去。由于您没有将“无”武器和盔甲存储在全局哈希中,因此这种情况更有可能发生。
尝试打印这两个“None”对象的指针,然后打印对象成员
weapon
或cArmour
的指针值。I'm guessing either
weapon
orcArmour
is null or point nowhere.That's all more likely since you're not storing your "None" weapon and armor in your global hash.
Try printing out the pointer for those two "None" objects, then the pointer values for the object members
weapon
orcArmour
.