C语言 收到“错误:‘函数’的类型冲突”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12992505/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 04:11:03  来源:igfitidea点击:

Receiving "error: conflicting types for 'function'"

cfunctioncompiler-errors

提问by Thomas Preston

I am creating a program to convert from binary, decimal, hex, and oct to any one of those options. For hex, I need a way to format values greater than 9 into one of A, B, C, D, E, F. Since this will be repeated in several functions, I decided to make the following function:

我正在创建一个程序来从二进制、十进制、十六进制和八进制转换为这些选项中的任何一个。对于十六进制,我需要一种将大于 9 的值格式化为 A、B、C、D、E、F 之一的方法。由于这将在几个函数中重复,我决定创建以下函数:

char hexRepresentation(double n){
    if(n > 9){
        if(n==10) return 'A';
        if(n==11) return 'B';
        if(n==12) return 'C';
        if(n==13) return 'D';
        if(n==14) return 'E';
        if(n==15) return 'F';
    }

    return (char)n;
}

However, when I try to compile, I receive the error

但是,当我尝试编译时,我收到错误

conflicting types for 'hexRepresentation'

'hexRepresentation' 的冲突类型

I'm utterly new at C, coming from Java, and am banging my head against a wall over what should be the simplest things to implement. Any help would be greatly appreciated!

我是 C 语言的新手,来自 Java,并且正在努力解决什么应该是最简单的事情来实现。任何帮助将不胜感激!

回答by Julio Moreno

You don't get a declaration kind-of error because in C, when you do not forward declare a function most compilers assume an extern function that returns an int type. Actually the compiler should warn you about this (most do). Then later on when the compiler actually reaches the function implementation it finds a different return type, in this case a char, and then throws the "conflicting type" error. Just forward declare all the functions to avoid this kind of errors.

你不会得到一个声明类型的错误,因为在 C 中,当你不前向声明一个函数时,大多数编译器都会假设一个返回 int 类型的 extern 函数。实际上编译器应该警告你这一点(大多数都这样做)。然后,当编译器实际到达函数实现时,它会找到不同的返回类型,在本例中为 char,然后抛出“冲突类型”错误。只需转发声明所有函数即可避免此类错误。

About what would be the best way somthing like following code woudl yield a similar result:

关于什么是最好的方法,比如下面的代码会产生类似的结果:

if (n > 9)
{
   return('A' + (n - 10));
}

回答by askmish

Whatever you are doing in the code posted, you are not using doubledatatype at all, within the function. And from the function return type, it appears you will never see the return value >127.

无论您在发布的代码中做什么,您都没有double在函数内使用数据类型。从函数返回类型来看,您似乎永远不会看到返回值 >127。

This code highlights some issues:(This is just illustrative)

这段代码突出了一些问题:(这只是说明性的)

char hexRepresentation(double n){
    if(n > 9){//comparing (double>int). Bad.
        if(n==10) return 'A';
        if(n==11) return 'B';
        if(n==12) return 'C';//You wrote if(double == int). Bad.
        if(n==13) return 'D';
        if(n==14) return 'E';
        if(n==15) return 'F';
    }

    return (char)n; //again unsafe downgrade from 8 bytes double to 1 byte char.
}

Even if you fix your compiler error, you may not get the desired results always, due to such dangerous usage of datatype in the function.

即使您修复了编译器错误,由于函数中数据类型的这种危险使用,您也可能无法始终获得所需的结果。

To know why its bad, look here:

要知道为什么它不好,请看这里:

http://www.cygnus-software.com/papers/comparingfloats/Comparing%20floating%20point%20numbers.htm

http://www.cygnus-software.com/papers/comparingfloats/Comparing%20floating%20point%20numbers.htm

I would use fabs(n)instead of neverywhere in that function body.

我会在该函数体中fabs(n)n任何地方使用而不是。

And the error "conflicting types for 'hexRepresentation'" will be shown when there exists a prior declaration or definition of the same named function hexRepresentationbefore this function definition. Also, if you do not declare a function and it only appears after being called, it is automatically assumed to be intby the compiler.

hexRepresentation在此函数定义之前存在同名函数的先前声明或定义时,将显示错误“'hexRepresentation'的类型冲突” 。另外,如果你没有声明一个函数并且它只在被调用后出现,int编译器会自动假定它是。

So, declare and define your function before main() or declare before main() and define the function anywhere else in the file, but, with the same function prototype.

因此,在 main() 之前声明和定义您的函数或在 main() 之前声明并在文件中的任何其他地方定义该函数,但使用相同的函数原型。

Do:

做:

 char hexRepresentation(double); //Declaration before main
 main()
 {
   ...
 }
 char hexRepresentation(double n){//Definition after main
 ...
 }

Or

或者

 char hexRepresentation(double n){ //Declaration and definition before main
  ...
 }

 main()
 {
    ...
 }

回答by Omkant

Just create one stack ( for char) which has push and pop functions. I am not returning the value just printing it there in the same function. (have implemented for integral values only)

只需创建一个具有推送和弹出功能的堆栈(用于字符)。我不返回值只是在同一个函数中打印它。(仅对整数值实施)

#define max 100
char a[max];

void hex(int n)
{
 while(n>0)
 {
 int rem = n%16;
 if (rem<10)
   push(rem+'0');
 elsif(rem >=10 && rem <16)
   {
     switch(rem)
     {
       case 10:
         push('A');break;
         case 11:
         push('B');break;
         case 12:
         push('C');break;
         case 13:
         push('D');break;
         case 14:
         push('E');break;
         case 15:
         push('F');break;
     }
    else
       n=n/16;
   }
   }
   i=0;
   while(top>-1)
     a[i++]=pop();
   i=0;
   while(a[i]!='##代码##')
     printf("%c",a[i++]);
}

Is it helpful ?

有用吗?