调用构造函数后程序崩溃

Program crashes after constructor is called

本文关键字:崩溃 程序 构造函数 调用      更新时间:2023-10-16
 template<class item_type>  struct node{
     item_type x;
     node<item_type> *left;
     node<item_type> *right;
     int Get_Height();
     int Get_Num_Nodes();  };
 template<class item_type, class param>  class Tree{
        node<item_type> *root;
    public: // some functions
      Tree(int roo);

我有一个将节点作为叶子的树类。Tree(int roo( 是构造函数。

template<class item_type, class param>
Tree<item_type, param>::Tree(int roo)
{
    this->root->x=roo;
    this->root->left=NULL;
    this->root->right=NULL;
}

这是构造函数的暗示。我还尝试同时省略 root->left=NULL 和 right 相同,以及没有构造函数并使用默认构造函数。

当我在main()中运行Tree<int, int> durr(1);时,所有这些似乎都会使我的程序崩溃

似乎看不出问题,我是一个新手程序员。任何帮助将不胜感激。

使用 this->root = new node<item_type> 初始化*root