You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

构建BST时遇'build_bst'重定义错误,求排查解决

错误原因与解决办法

你遇到的'build_bst': redefinition, different basic types错误,核心原因是函数声明缺失导致的编译器默认声明与实际定义冲突,和参数格式无关——你的参数传递是正确的(&root是Node**类型,和函数参数struct Node**兼容,因为Node是struct Node的typedef别名)。

在C语言中,如果函数在被调用前没有提前声明或定义,编译器会自动生成一个默认声明:默认返回值类型为int,参数类型根据调用时的参数推断。你的代码里main函数先调用了build_bst,但函数定义在main之后,编译器会先生成一个int build_bst(Node**)的默认声明,之后看到实际定义的void build_bst(struct Node**),两者返回类型(int vs void)不一致,因此报重定义错误。

解决方法有两种:

方法1:提前声明函数

在main函数之前添加build_bst的函数声明:

typedef struct Node {
    char word[11];
    char description[101];
    struct Node* right;
    struct Node* left;
} Node;

// 提前声明函数
void build_bst(struct Node** root);
// 或者用typedef后的类型:void build_bst(Node** root);

int main (void) {
    Node *root = NULL;
    build_bst(&root);
}

void build_bst(struct Node** root) {
    /* ... */
}

方法2:将函数定义移到main之前

把build_bst的完整定义放到main函数前面,让编译器在调用前就能看到完整的函数信息:

typedef struct Node {
    char word[11];
    char description[101];
    struct Node* right;
    struct Node* left;
} Node;

void build_bst(struct Node** root) {
    /* ... */
}

int main (void) {
    Node *root = NULL;
    build_bst(&root);
}

内容的提问来源于stack exchange,提问作者Kathleen Hammett

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.28 18:50:13