返回类型冲突

发布于 2024-08-27 01:00:05 字数 882 浏览 6 评论 0原文

我正在做一个递归程序,并且收到有关冲突类型的错误:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

好久不见√ 2024-09-03 01:00:05

如果没有原型,则无法在正确定义之前使用函数。 buddy_malloc() 在原型化或定义之前使用 buddy_findout(),编译器会将其视为定义。

在定义 buddy_Malloc() 之前创建 buddy_findout() 原型,或在定义 buddy_Malloc() 之前定义 buddy_findout()

我建议使用原型,即:

void *buddy_Malloc(int);
void *buddy_findout(int, int);

... 就在最后一个 #include 下面,

这可以避免您定义事物的顺序出现任何混乱。另外,在指定大小时,请考虑使用 size_t (架构上可用的最大无符号整数类型)而不是有符号整数。

这是使用这两种方法的代码(已更正)。方法 1 - 使用原型:

void *buddy_Malloc(int);
void *buddy_findout(int, int);

void* buddyMalloc(int req_size)
{ 
          //Do something here//
         return buddy_findout(original_index,req_size); //This is the recursive fn I 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);
         }
}

而方法 2,只是重新排序:

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);
         }
}

void* buddyMalloc(int req_size)
{ 
          //Do something here//
         return buddy_findout(original_index,req_size); //This is the recursive fn I call//
}

在某些圈子里,静态函数不需要原型被认为是一种艺术,这表明该程序是在在编写代码之前某人的头脑。无论哪种方式,我都没什么可说的,除了向那些仍在学习 C 的具体细节的人推荐静态函数的原型之外。

如果 buddy_* 将被暴露要使用其他模块,您确实需要原型。很难判断您是否希望这些是静态的。

编辑:

如果您将原型放入外部头文件中,则需要使用包含防护来确保每个模块仅包含它们一次(并且不会重新包含它们)将它们定义为完全相同的东西)。

下面是一个示例 buddy.h

#ifndef BUDDY_H
#define BUDDY_H

void *buddy_Malloc(int);
void *buddy_findout(int, int);

#endif /* BUDDY_H */

然后,预处理器将阻止您的模块抛出该错误。

You can't use a function prior to its proper definition without a prototype. buddy_malloc() is using buddy_findout() before it is prototyped or defined, which the compiler will treat as a definition.

Prototype buddy_findout() prior to defining buddy_Malloc(), or define buddy_findout() prior to defining buddy_Malloc().

I suggest prototypes, i.e:

void *buddy_Malloc(int);
void *buddy_findout(int, int);

... 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:

void *buddy_Malloc(int);
void *buddy_findout(int, int);

void* buddyMalloc(int req_size)
{ 
          //Do something here//
         return buddy_findout(original_index,req_size); //This is the recursive fn I 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);
         }
}

And method 2, just re-ordering:

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);
         }
}

void* buddyMalloc(int req_size)
{ 
          //Do something here//
         return buddy_findout(original_index,req_size); //This is the recursive fn I call//
}

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:

#ifndef BUDDY_H
#define BUDDY_H

void *buddy_Malloc(int);
void *buddy_findout(int, int);

#endif /* BUDDY_H */

The preprocessor will then keep your modules from throwing that error.

缺⑴份安定 2024-09-03 01:00:05

我想这一定是因为缺少原型。

添加函数原型

void *buddy_findout(int current_index,int req_size);

在函数buddyMalloc

I guess this has to be with missing prototypes.

Add the function prototype of

void *buddy_findout(int current_index,int req_size);

before the function buddyMalloc

停顿的约定 2024-09-03 01:00:05

buddy_findout 被声明为返回一个指向 void 的指针,但在一个地方您试图返回 selected,它是一个指向 char 的指针。正如其他人已经指出的那样,您还需要 buddy_findout 的原型:

void *buddy_findout(int, int);

void *buddy_malloc(int req_size) { 
    return buddy_findout(original_index,req_size);
}

void *buddy_findout(int current_index, int req_size) {
// ...
    if (current_index == original_index)
        return (void *)selected;
// ...
}

buddy_findout is declared to return a pointer to void, but in one place you're attempting to return selected, which is a pointer to char. As others have already pointed out, you also need a prototype for buddy_findout:

void *buddy_findout(int, int);

void *buddy_malloc(int req_size) { 
    return buddy_findout(original_index,req_size);
}

void *buddy_findout(int current_index, int req_size) {
// ...
    if (current_index == original_index)
        return (void *)selected;
// ...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文