gpt4 book ai didi

c++ - 读取结构时出现未处理的异常

转载 作者:行者123 更新时间:2023-11-28 03:51:14 25 4
gpt4 key购买 nike

程序在这一行抛出未处理的异常:

}else if(s == "backpack"){
cout << "\nEquipped items: " << endl;
cout << weapon->Name << endl << cArmour->Name << endl; //this line

它打印“Equipped items:”然后抛出异常。这个文件 - 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

返回所有武器的 map :

//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;
}

然后将这些 map 作为参数传递给上面显示的播放器构造函数。

最佳答案

我猜 weaponcArmour 为空或指向任何地方。

这更有可能,因为您没有将“无”武器和盔甲存储在全局哈希中。

尝试打印出这两个“无”对象的指针,然后是对象成员 weaponcArmour 的指针值。

关于c++ - 读取结构时出现未处理的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5448153/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com