我似乎无法让这个递归函数正确编译,我也不确定为什么。 代码如下:
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) {
要解决此问题,您可以:
在第 96 行之前的某处显式声明:
void point_forward(mem_ptr m);
这将告诉编译器如何处理point_forward()
当它看到它在第 96 行,即使它还没有看到函数实现。或者,在第 96 行以上定义整个函数(将函数定义从第 134 行向前移动到第 96 行以上)。
这里有点more about declaring functions .
通常,对于样式,我会:
如果您不想在任何其他 C 文件中使用
point_forward()
,请完整定义它:static void point_forward(mem_ptr m) { ..function body goes here..}
在源文件的顶部。
如果你想在其他C文件中使用
point_forward()
,提出前向声明:void point_forward(mem_ptr m);
在要包含的其他文件的头文件中。
关于c - 注意 : previous implicit declaration of ‘point_forward’ was here,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16182115/