返回类型冲突
我正在做一个递归程序,并且收到有关冲突类型的错误:
void* buddyMalloc(int req_size)
{
// Do something here
return buddy_findout(original_index,req_size); // This is the recursive call
}
void *buddy_findout(int current_index,int req_size)
{
char *selected = NULL;
if(front!=NULL)
{
if(current_index==original_index)
{
// Do something here
return selected;
}
else
{
// Do Something here
return buddy_findout(current_index+1,req_size);
}
}
else
{
return buddy_findout(current_index-1,req_size);
}
}
错误:
buddy.c: At top level: buddy.c:76: error: conflicting types for ‘buddy_findout’ buddy.c:72: note: previous implicit declaration of ‘buddy_findout’ was here
请注意我在其中定义的文件 buddy.c 不包含 main 并与其他几个 .c 文件链接。
I am doing a recursive program and I am getting an error about conflicting types:
void* buddyMalloc(int req_size)
{
// Do something here
return buddy_findout(original_index,req_size); // This is the recursive call
}
void *buddy_findout(int current_index,int req_size)
{
char *selected = NULL;
if(front!=NULL)
{
if(current_index==original_index)
{
// Do something here
return selected;
}
else
{
// Do Something here
return buddy_findout(current_index+1,req_size);
}
}
else
{
return buddy_findout(current_index-1,req_size);
}
}
Error:
buddy.c: At top level: buddy.c:76: error: conflicting types for ‘buddy_findout’ buddy.c:72: note: previous implicit declaration of ‘buddy_findout’ was here
Please note the file buddy.c in which I am defining this does not contain main and is linked with several other .c files.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果没有原型,则无法在正确定义之前使用函数。
buddy_malloc()
在原型化或定义之前使用buddy_findout()
,编译器会将其视为定义。在定义
buddy_Malloc()
之前创建buddy_findout()
原型,或在定义buddy_Malloc()
之前定义buddy_findout()
。我建议使用原型,即:
... 就在最后一个
#include
下面,这可以避免您定义事物的顺序出现任何混乱。另外,在指定大小时,请考虑使用
size_t
(架构上可用的最大无符号整数类型)而不是有符号整数。这是使用这两种方法的代码(已更正)。方法 1 - 使用原型:
而方法 2,只是重新排序:
在某些圈子里,静态函数不需要原型被认为是一种艺术,这表明该程序是在在编写代码之前某人的头脑。无论哪种方式,我都没什么可说的,除了向那些仍在学习 C 的具体细节的人推荐静态函数的原型之外。
如果
buddy_*
将被暴露要使用其他模块,您确实需要原型。很难判断您是否希望这些是静态的。编辑:
如果您将原型放入外部头文件中,则需要使用包含防护来确保每个模块仅包含它们一次(并且不会重新包含它们)将它们定义为完全相同的东西)。
下面是一个示例
buddy.h
:然后,预处理器将阻止您的模块抛出该错误。
You can't use a function prior to its proper definition without a prototype.
buddy_malloc()
is usingbuddy_findout()
before it is prototyped or defined, which the compiler will treat as a definition.Prototype
buddy_findout()
prior to definingbuddy_Malloc()
, or definebuddy_findout()
prior to definingbuddy_Malloc()
.I suggest prototypes, i.e:
... Just under your last
#include
This avoids any confusion on the order that you define things. Also, consider using
size_t
(the largest unsigned int type available on the architecture) instead of signed integers when specifying sizes.Here is your code (corrected) using both methods. Method 1 - using prototypes:
And method 2, just re-ordering:
In some circles it is sort of considered an art to not need prototypes for static functions, it kind of demonstrates the program was planned out in someone's head before code was written. I don't have much to say about that either way, other than recommending prototypes even for static functions to those who are still learning the nuts and bolts of C.
If
buddy_*
is going to be exposed for other modules to use, you really need prototypes. Its hard to tell if you intend these to be static or not.Edit:
If you are putting the prototypes in an external header file, you need to use include guards to ensure that each module includes them only once (and doesn't re-define them to be the exact same thing).
Here is a sample
buddy.h
:The preprocessor will then keep your modules from throwing that error.
我想这一定是因为缺少原型。
添加函数原型
在函数
buddyMalloc
前I guess this has to be with missing prototypes.
Add the function prototype of
before the function
buddyMalloc
buddy_findout
被声明为返回一个指向 void 的指针,但在一个地方您试图返回selected
,它是一个指向 char 的指针。正如其他人已经指出的那样,您还需要 buddy_findout 的原型:buddy_findout
is declared to return a pointer to void, but in one place you're attempting to returnselected
, which is a pointer to char. As others have already pointed out, you also need a prototype forbuddy_findout
: