【问题标题】:Error : Conflicting types for 'remove'错误:“删除”的类型冲突
【发布时间】:2016-10-13 12:34:25
【问题描述】:

我在定义删除函数的行中收到错误“'remove' 的类型冲突”。 此错误的大多数情况是在声明之前调用函数时发生的。 但是,我在 main 函数中调用了我的 remove(),同时事先定义了它。 那么,为什么会报错?!?

    #include<stdio.h>
    #include<stdbool.h>

    struct node
    {
        int data;
        struct node* left;
        struct node* right;
    };

    struct node* newNode(int x)
    {
        struct node* temp=(struct node*)malloc(sizeof(struct node));
        temp->data=x;
        temp->left=NULL;
        temp->right=NULL;
        return temp;
    }

    struct node* insert(struct node* root,int x)
    {
        if (root==NULL)
            root=newNode(x);
        else if (x<=root->data)
            root->left=insert(root->left,x);
        else
            root->right=insert(root->right,x);
        return root;
    }

    struct node* remove(struct node* root,int x)
    {
        if (root==NULL)
            printf("Node not found !\n");
        else if (x==root->data)
        {
            struct node* temp=root;
            root=NULL;
            free(temp);
            printf("Node removed !\n");
        }
        else if (x<=root->data)
            root->left=remove(root->left,x);
        else
            root->right=remove(root->right,x);
        return root;
    }

    bool search(struct node* root,int x)
    {
        if (root==NULL)
            return false;
        else if (x==root->data)
            return true;
        else if (x<=root->data)
            return search(root->left,x);
        else
            return search(root->right,x);
    }

    void main()
    {
        struct node* root=NULL;
        root=insert(root,20);
        root=remove(root,10);
        root=insert(root,8);
        root=remove(root,10);
        root=insert(root,22);
        root=remove(root,22);
        root=insert(root,21);
        root=remove(root,10);
        root=insert(root,12);
        root=remove(root,12);
        root=insert(root,16);
        root=remove(root,10);
        root=insert(root,0);
        root=remove(root,10);
        root=insert(root,11);
        root=remove(root,10);
        root=remove(root,11);
        printf(search(root,10)?"Found\n":"Not Found\n");
        printf(search(root,20)?"Found\n":"Not Found\n");
        printf(search(root,11)?"Found\n":"Not Found\n");
        printf(search(root,17)?"Found\n":"Not Found\n");
    }

【问题讨论】:

    标签: c


    【解决方案1】:

    编译你的代码时,我得到了这个:

    /tmp/x1.c:32: error: conflicting types for ‘remove’
    /usr/include/stdio.h:154: error: previous declaration of ‘remove’ was here
    

    如您所见,stdio.h 中声明了一个名为remove 的函数。这与您的定义相冲突。

    您需要重命名您的函数,使其不会与stdio.h 中的定义冲突。

    【讨论】:

    • 您先生,真是个天才!然后一吨。
    • @iamrkcheers 很高兴我能提供帮助。如果您觉得有用,请随时 accept this answer
    猜你喜欢
    • 2016-02-07
    • 2013-03-25
    • 1970-01-01
    • 2014-03-10
    • 2012-10-04
    • 2013-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多