调用构造函数后程序崩溃

时间:2013-12-13 18:17:26

标签: c++ class pointers tree nodes

 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类,它以节点作为叶子。 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和同时使用右边和右边两个,同时没有构造函数并使用默认值。

当我在Tree<int, int> durr(1);

中运行main()时,所有这些似乎都会导致程序崩溃

我似乎无法看到问题而且我是一名新手程序员。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

使用*root

初始化this->root = new node<item_type>
相关问题