gpt4 book ai didi

c - 注意: previous implicit declaration of ‘point_forward’ was here

转载 作者:行者123 更新时间:2023-11-30 17:03:08 31 4
gpt4 key购买 nike

我似乎无法让这个递归函数正确编译,我也不知道为什么。代码如下:

void point_forward (mem_ptr m) {
mem_ptr temp;
temp = m->next;
if (temp->next != NULL) point_forward(temp);
m->next = temp->next;
}

我的编译器返回这个:

mm.c:134:6: warning: conflicting types for ‘point_forward’ [enabled by default]
mm.c:96:2: note: previous implicit declaration of ‘point_forward’ was here

最佳答案

关键在于:

previous implicit declaration of ‘point_forward’ was here

在第 96 行,您有:

point_forward(m); // where m is a mem_ptr;

由于编译器尚未看到 point_forward(m) 的函数声明,因此它“隐式定义”(即假设)一个返回 int 的函数:

int point_forward(mem_ptr m);

这与后面的定义冲突:

void point_forward (mem_ptr m) {
<小时/>

要解决此问题,您可以:

  1. 在第 96 行之前放置一个显式声明:void point_forward(mem_ptr m);这将告诉编译器在看到point_forward()时如何处理它位于第 96 行,即使它还没有看到函数实现。

  2. 或者,在第 96 行上方定义整个函数(将函数定义从第 134 行向前移动到第 96 行上方)。

这里有一点more about declaring functions .

一般来说,为了风格,我会:

  • 如果您不想在任何其他 C 文件中使用 point_forward(),请完整定义它:

    static void point_forward(mem_ptr m) { ..函数体放在这里.. }

    位于源文件的顶部。

  • 如果要在其他 C 文件中使用 point_forward(),请添加前向声明:

    void point_forward(mem_ptr m);

    在头文件中供其他文件包含。

关于c - 注意: previous implicit declaration of ‘point_forward’ was here,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36267361/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com