小编Zoz*_*zez的帖子

如何修复“抛出'std::logic_error'what()的实例后调用终止:basic_string::_M_construct null not valid”异常?

我下面的代码由二叉树数据结构组成:

#include <bits/stdc++.h>
#define DEFAULT_NODE_VALUE 0
using namespace std;

template <class T>
class node{
public:
    T val;
    node* right = 0;
    node* left = 0;
    node(T a):val(a){}

};

template <class T>
class tree{
public:
    node<T>* root = new node<T>(DEFAULT_NODE_VALUE);
    tree(T inp_val){
        root->val = inp_val; 
    }

    void inorder_traverse(node<T>* temp){
        if (!temp)
            return;
        inorder_traverse(temp->left);
        cout << temp->val << " -> ";
        inorder_traverse(temp->right);
    }
    void inorder_traverse(){
        inorder_traverse(root);
    }
    
};

int main()
{   
    tree<string> my_tree("mantap");
    my_tree.root->right = new node<string>("ok");
    my_tree.root->left = new node<string>("haha");

    my_tree.inorder_traverse(); …
Run Code Online (Sandbox Code Playgroud)

c++ runtime-error runtime exception

0
推荐指数
1
解决办法
1073
查看次数

标签 统计

c++ ×1

exception ×1

runtime ×1

runtime-error ×1