Domanda

Il programma lancia un'eccezione non gestita su questa riga:

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

Stampa "articoli equipaggiati:" quindi lancia un'eccezione. Questo file - Player.h - Include Library.h, che a sua volta include Globals.h, che ha le strutture:

struct sWeapon{
    std::string Name;
    int Damage;
};

struct sArmour{
    std::string Name;
    int AP;
};

Nel costruttore del giocatore, crea gli oggetti 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"];
}

All'inizio dell'intero programma, chiama INIT_ASHAPONS e 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

che restituiscono la mappa di tutte le armi:

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

Quindi passa quelle mappe come argomenti al costruttore del giocatore mostrato sopra.

È stato utile?

Soluzione

Nemmeno io weapon o cArmour non è nullo o punto da nessuna parte.

È tutto più probabile dal momento che non stai conservando l'arma e l'armatura "nessuna" nel tuo hash globale.

Prova a stampare il puntatore per quei due oggetti "nessuno", quindi i valori del puntatore per i membri dell'oggetto weapon o cArmour.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top