“'的错误冲突类型是什么意思? [英] What does error conflicting types for '' mean?

查看:174
本文介绍了“'的错误冲突类型是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到一条错误,上面写着错误:'____'的类型冲突。这是什么意思?

i got an error that said "error: conflicting types for '____'. What does that mean?

推荐答案


Quickfix:



确保您的函数在调用之前只声明一次。例如,更改:

Quickfix:

Make sure that your functions are declared once and only once before they are called. For example, change:

main(){ myfun(3.4); }
double myfun(double x){ return x; }

收件人:

double myfun(double x){ return x; }
main(){ myfun(3.4); }

或者添加一个单独的函数声明:

Or add a separate function declaration:

double myfun(double x);
main(){ myfun(3.4); }
double myfun(double x){ return x; }




错误的可能原因



Possible causes for the error


  1. 在宣布之前调用了函数

  2. 定义的函数会覆盖在包含的标题中声明的函数。

  3. 函数定义为tw同一档中的冰

  4. 声明和定义不匹配

  5. 包含标题中的声明冲突

  1. Function was called before being declared
  2. Function defined overrides a function declared in an included header.
  3. Function was defined twice in the same file
  4. Declaration and definition don't match
  5. Declaration conflict in the included headers



真正发生的事情



错误:'foo'的冲突类型表示函数是使用不同的类型签名多次定义的。

What's really going on

error: conflicting types for ‘foo’ means that a function was defined more than once with different type signatures.

包含两个具有相同名称但返回类型不同的函数的文件会抛出此错误,例如:

A file that includes two functions with the same name but different return types would throw this error, for example:

int foo(){return 1;}
double foo(){return 1.0;}

的确,在使用GCC编译时,我们会收到以下错误:

Indeed, when compiled with GCC we get the following errors:

foo.c:5:8: error: conflicting types for ‘foo’
 double foo(){return 1.0;}
        ^
foo.c:4:5: note: previous definition of ‘foo’ was here
 int foo(){return 1;}
     ^

现在,如果我们有一个文件,其中包含两个具有相同名称的函数定义

Now, if instead we had a file with two function definitions with the same name

double foo(){return 1;}
double foo(){return 1.0;}

我们会收到重新定义错误:

We would get a 'redefinition' error instead:

foo.c:5:8: error: redefinition of ‘foo’
 double foo(){return 1.0;}
        ^
foo.c:4:8: note: previous definition of ‘foo’ was here
 double foo(){return 1;}
        ^



隐式函数声明



那么为什么以下代码抛出错误:'foo'的冲突类型

 main(){ foo(); }
 double foo(){ return 1.0; }

原因是隐式函数声明

当编译器第一次遇到 main 函数中的 foo()时,它将会假设函数 foo 的类型签名为 int foo()。默认情况下,假定隐式函数返回整数,输入参数类型是从您传递给函数的内容派生的(在本例中,没有)。

When the compiler first encounters foo() in the main function, it will assume a type signature for the function foo of int foo(). By default, implicit functions are assumed to return integers, and the input argument types are derived from what you're passing into the function (in this case, nothing).

显然,编译器做出这个假设是错误的,但是C(以及Objective-C)语言的规范是陈旧的,胡思乱想的,而且不是很聪明。也许通过在当天减少编译器复杂性来隐式声明函数节省了一些开发时间,但是现在我们遇到了一个本来应该从未进入语言的可怕特性。实际上,在C99中隐式声明是非法的。

Obviously, the compiler is wrong to make this assumption, but the specs for the C (and thus Objective-C) language are old, cranky, and not very clever. Maybe implicitly declaring functions saved some development time by reducing compiler complexity back in the day, but now we're stuck with a terrible feature that should have never made it into the language. In fact, implicit declarations were made illegal in C99.

这就是说,一旦你知道发生了什么,就应该很容易找出问题的根本原因。

That said, once you know what's going on, it should be easy to dig out the root cause of your problem.

这篇关于“'的错误冲突类型是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆