2014-11-09 27 views
-1

我正在尝试为需要输入并将其放入二进制树的学校中的课程创建程序。当我编译我得到错误“无法转换'树**'到'树*'在作业中”。它编译我是否注释掉InsertNode方法。无法在作业中将'树**'转换为'树*'

//The main method to create the tree 
void CreateTree() 
{ 
    char list[MAX_SIZE]; 

    string line; 
    getline(cin,line); 
    strcpy(list,line.c_str()); 
    cout << list << endl; 

    Tree* root = new Tree; 
    root->data = list[0]; 
    root->right = NULL; 
    root->left = NULL; 

    for(int i=1; i<MAX_SIZE; i++) 
    { 
     InsertNode(root, list[i]); 
    } 

} 

//The method to insert the next char into the tree 
void InsertNode(Tree* root, char n) 
{ 
    Tree* curr = &root; //the error is here 
    if(curr==NULL) 
     curr->data=n; 
    else if(curr->data<=n) 
    { 
     curr=curr->right; 
     InsertNode(root, n); 
    } 
    else if(curr->data>n) 
    { 
     curr=curr->left;  
     InsertNode(root, n); 
    } 
} 

我意识到其他一些代码可能无法按预期工作,但我只是想帮助我收到的错误。

回答

4

而不是

Tree* curr = &root; 

你需要

Tree* curr = root; 

rootcurr应该是指针到树(即其类型为Tree *)。但是&root的意思是“root”的地址,因此它是指向树的指针(即,Tree **)。错误消息告诉你,你不能交换Tree *Tree **这两种类型。