【问题标题】:terminate called after throwing an instance of 'std::logic_error' what(): basic_string::_M_construct null not valid, class string variable抛出“std::logic_error”实例后调用终止 what(): basic_string::_M_construct null 无效,类字符串变量
【发布时间】:2022-01-11 05:54:27
【问题描述】:

代码如下:

#include <sstream>
#include <string>
#include <iostream>
#include <fstream>
#include <cstring>

using namespace std;

class Monster{
public:
    int health;
    int speed;
    int damage;
    string name;

    Monster(int inHealth, int inSpeed,int inDamage, string inName){
        health = inHealth;
        speed = inSpeed;
        damage = inDamage;
        name = inName;
        cout<< name<< " created"<<endl;
    }

    int Attack(Monster * opponent){
        opponent->health = opponent->health - this->damage;
        cout<< this->name << " attacked " << opponent->name << " for "<< this->damage <<"     damage!"<<endl;
        cout<<"New " << opponent->name <<" health is: "<< opponent->health<<endl;
    }

    string getName(){
        //if (name != string()){
             return name;
       // }
       // else{
        //    return("error: name is null");
      //  }
    }
};

class Node{
public:
    Monster* mob;
    Node* left;
    Node* right;
};

class BST{
public:
    Node* root;

    BST(){
        root = NULL;
    }

    void add(int inHealth, int inSpeed,int inDamage, string inName){
        Node* n = new Node();
        Monster holder = Monster(inHealth, inSpeed, inDamage, inName);
        n->mob = &holder;
        if (root == NULL) {
            root = n;
            cout<<"was null"<<endl;
        }
        else{
            Node * iter = root;
            while(1){
                if(inSpeed > inDamage){
                    if(iter->left==NULL){
                        iter->left=n;
                        return ;
                    }
                    else{
                        iter = iter->left;
                    }
                }
                else{
                    if(iter->right==NULL){
                        iter->right=n;
                        return ;
                    }
                    else{
                        iter = iter->right;
                    }
               }
            }
        }
    }
};

int main()
{
    ifstream inFile;
    ofstream outFile;
    inFile.open("Mobs.txt");
    outFile.open("Score.txt");

    BST dungeon = BST();

    string nameHolder;
    string healthHolder;
    string damageHolder;
    string speedHolder;
    int healthHold;
    int damageHold;
    int speedHold;

    int weapon = 3;
    int pSpeed;
    int pDamage;
    if(weapon = 1){ //Knife
        pSpeed = 75;
        pDamage = 15;
    }
    else if(weapon = 2){ //Axe
        pSpeed = 1;
        pDamage = 60;
    }
    else{ //Sword
        pSpeed = 50;
        pDamage = 50;
    }

    Monster player = Monster(100, pDamage, pSpeed, "Player");

    while(getline(inFile, nameHolder, ',')) {
        getline(inFile, healthHolder, ',');
        getline(inFile, speedHolder, ',');
        getline(inFile, damageHolder);
        healthHold = stoi(healthHolder);
        damageHold = stoi(damageHolder);
        speedHold = stoi(speedHolder);
        dungeon.add(healthHold, speedHold,damageHold, nameHolder);
    }

    Monster *testMon = new Monster(100, 10, 10, "TestMob");
    Monster *currentMonster = dungeon.root->mob;

    cout<<"here1"<<endl;
    cout<< dungeon.root->mob->name<<endl;
    cout<< testMon->getName()<<endl;
    cout<< currentMonster->getName() <<endl;
    cout<<"here2"<<endl;
    while(player.health >0){
        cout<<"here"<<endl;
        if(player.speed > currentMonster->speed){
            player.Attack(currentMonster);
        }
        else(currentMonster->Attack(&player));
    }

    return 0;
}

控制台输出是:

Player created
Green Slime created
was null
Blue Slime created
Blue Slime created
Red Slime created
Red Slime created
Fire Golem created
Ice Golem created
Water Golem created
Fire Elemental created
Ice Elemental created
Water Elemental created
TestMob created
here1
terminate called after throwing an instance of 'std::logic_error'
  what():  basic_string::_M_construct null not valid

Process returned 3 (0x3)   execution time : 0.364 s
Press any key to continue.

不确定是什么导致了这个错误,似乎是因为Monster 名称字符串没有被正确创建,但我不确定。

【问题讨论】:

    标签: c++ string binary-search-tree


    【解决方案1】:
    void add(int inHealth, int inSpeed,int inDamage, string inName){
    
    // ...
    
        Monster holder = Monster(inHealth, inSpeed, inDamage, inName);
    

    这个holder 对象被声明为add() 中具有自动作用域的对象。这就是这个声明的意思,当add() 返回时,holder 会自动销毁。

      n->mob = &holder;
    

    一个指向这个对象的指针被存储在这个Node中。

    稍后的代码尝试访问此指针所引用的对象,但在add() 已经返回之后。一旦add() 返回,指针就指向一个被破坏的对象,所有后续访问它的尝试都会导致未定义的行为,并且可能是导致崩溃的原因。

    整个二叉树最终都带有指向被破坏对象的指针。

    【讨论】:

      猜你喜欢
      • 2019-02-08
      • 2023-01-17
      • 2019-10-26
      • 2017-04-11
      • 2017-12-13
      • 2012-07-27
      • 2020-10-15
      • 1970-01-01
      • 2019-06-23
      相关资源
      最近更新 更多