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

发布于 2024-10-26 21:42:17 字数 1985 浏览 4 评论 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;
}

然后将这些地图作为参数传递给上面所示的玩家构造函数。

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.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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

苯莒 2024-11-02 21:42:17

我猜测 weaponcArmour 为 null 或无处可去。

由于您没有将“无”武器和盔甲存储在全局哈希中,因此这种情况更有可能发生。

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文