0

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

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

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

4

1 回答 1

2

我猜要么weaponcArmour为空,要么无处可去。

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

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

于 2011-03-27T08:50:57.850 回答