阅读结构时未处理的异常

时间:2011-03-27 08:47:12

标签: c++ exception structure unhandled

该程序在此行上抛出未处理的异常:

}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构造函数中,它创建struct对象:

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

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

1 个答案:

答案 0 :(得分:2)

我猜测weaponcArmour为空或无处可寻。

由于你没有在你的全局哈希中存储你的“无”武器和护甲,所以更有可能。

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