无法转换'树**'到树#'在任务中

时间:2014-11-09 00:43:48

标签: c++ pointers binary-tree

我正在尝试为学校的课程创建一个程序,该程序接受输入并将其放入二叉树中。当我编译我得到错误"无法转换'树**'到树#'在任务"。如果我注释掉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);
    }
}

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

1 个答案:

答案 0 :(得分:4)

而不是

Tree* curr = &root;

你需要

Tree* curr = root;

rootcurr应该是指向树的指针(即它们的类型为Tree *)。但是&root表示“root”的地址,因此它是指针指向树的指针(即Tree **)。错误消息告诉您无法交换Tree *Tree **两种类型。

相关问题