开发者

Unhandled exception while reading structures

开发者 https://www.devze.com 2023-02-19 20:29 出处:网络
The program throws an unhandled exception on this line: }else if(s == \"backpack\"){ cout << \"\\nEquipped items: \" << endl;

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.开发者_StackOverflow社区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.


I'm guessing either weapon or cArmour 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 or cArmour.

0

精彩评论

暂无评论...
验证码 换一张
取 消