C语言 C 警告冲突类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2431242/
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:50:28  来源:igfitidea点击:

C warning conflicting types

cwarnings

提问by ambika

my code is

我的代码是

void doc(){
          //mycode                
            return;
           }

my warning is

我的警告是

conflicting types for 'doc'

can anybody solve it.

任何人都可以解决它。

回答by Alok Singhal

In C, if you don't have a prototype for a function when you call it, it is assumed to return an intand to take an unspecified number of parameters. Then, when you later define your function as returning voidand taking no parameters, the compiler sees this as a conflict.

在 C 中,如果在调用函数时没有函数原型,则假定它返回 anint并采用未指定数量的参数。然后,当您稍后将函数定义为返回void且不带参数时,编译器会将其视为冲突。

Depending upon the complexity of your code, you can do something as simple as moving the definition of the function before its use, or add the function declarationin a header file and include it.

根据代码的复杂性,您可以做一些简单的事情,例如在使用之前移动函数的定义,或者在头文件中添加函数声明并包含它。

In any case, the net effect should be to make the function prototype available before it is being used.

无论如何,最终效果应该是在使用函数原型之前使其可用。

If you add

如果添加

void doc(void);

before the function use, you will have a prototype visible in scope, and your warning will go away.

在函数使用之前,您将在范围内看到一个原型,您的警告将消失。

I think this is the most likely cause for your warning. You could have an explicit incompatible declaration of docin your code, but we can't tell because you have not posted complete code.

我认为这是您发出警告的最可能原因。您的代码中可能有一个显式的不兼容声明doc,但我们无法判断,因为您尚未发布完整的代码。

回答by swapnil

try writing your doc function before your main function in your program file.

尝试在程序文件中的 main 函数之前编写 doc 函数。

回答by raj

u have declared it with some other type/signature and defined with some other type/signature..

你已经用其他类型/签名声明它并用其他类型/签名定义..

like

喜欢

int doc();
void doc()
{ 
}

will give u this warning.

会给你这个警告。

回答by Greg Hewgill

That's clearly not your complete code.

这显然不是您的完整代码。

However, that error means that there is another declaration for doc(perhaps a global variable? something in a header file?) that isn't a voidfunction that takes no parameters.

但是,该错误意味着doc(可能是全局变量?头文件中的某些内容?)的另一个声明不是不void带参数的函数。

回答by jag

"doc" is probably already declared with a different type... you should try to find the previous declaration !

“doc”可能已经用不同的类型声明了......你应该尝试找到以前的声明!

回答by AnT

You have either declared docbefore, or made a call to undeclared docthus forcing the compiler to deduce a probable parameter declaration for docfrom that call. Now, the definition of docthat you quoted is different from that previous declaration (either explicit or deduced by the compiler), which is what is reported as a "conflict".

doc之前已经声明过,或者调用了 undeclareddoc从而迫使编译器doc从该调用中推断出可能的参数声明。现在,doc您引用的定义与先前的声明(显式的或由编译器推断的)不同,后者被报告为“冲突”。

回答by abubacker

Make sure that you have not used doc any where in your code !, I think that only gives u trouble!

确保您没有在代码中的任何地方使用 doc!,我认为这只会给您带来麻烦!

回答by kc ochibili

Declaring the function above the usage solved it for me.

声明上面的函数为我解决了这个问题。

Example:

例子:

void recordAudio(){  //declaration 

    doStuff();
}


void initialise(){ 

    recordAudio();    // usage
}

as you can see above, the usage of recordAudio()is above where is is used.

正如你在上面看到的,使用的地方recordAudio()是上面使用的地方。

回答by Saidas S R

It is because you didn't create any function prototype before calling the function. So compiler assumes return type as int by default. But while defining function you have used return type as void, which is conflict to complier assumption. that is why you are getting error conflicting types for 'doc'

这是因为您在调用函数之前没有创建任何函数原型。因此,编译器默认假定返回类型为 int。但是在定义函数时,您使用了返回类型为 void,这与编译器假设相冲突。这就是为什么您会收到“doc”类型的错误冲突

You can solve it by adding a function prototype before main

可以通过在main之前添加函数原型来解决

#include <stdio.h>

void doc();

int main()
{
    doc();
}

void doc()
{
      //mycode                
        return;
}