0
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和both,並且沒有構造函數並使用默認構造函數。
所有這一切似乎當我main()
運行Tree<int, int> durr(1);
崩潰我的計劃,我似乎無法看到一個問題,我是新手程序員。任何幫助,將不勝感激。
'root'未初始化。 – chris
root是一個NULL ptr – Hamdor
@blub這有點不恰當。在發佈版本中,根可能只是指向垃圾,而不是NULL。 – IdeaHat