C语言 如何在C中对函数进行排序?“函数的先前隐式声明在这里”错误

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

How to sort functions in C? "previous implicit declaration of a function was here" error

cfunction

提问by Randalfien

I'm sure this has been asked before, but I couldn't find anything that would help me. I have a program with functions in C that looks like this

我确定以前有人问过这个问题,但我找不到任何可以帮助我的东西。我有一个带有 C 函数的程序,看起来像这样

function2(){
  function1()
}
function1 (){
  function2()
}

main () {
 function1()
}

It's more complicated than that, but I'm using recursion. And I cannot arrange the function in the file so that every function would only call functions that are specified above itself. I keep getting an error

它比那更复杂,但我正在使用递归。而且我无法在文件中安排函数,以便每个函数只能调用上面指定的函数。我不断收到错误

main.c:193: error: conflicting types for 'function2'
main.c:127: error: previous implicit declaration of 'function2' was here

How do I avoid this? Thanks in advance for suggestions and answers.

我如何避免这种情况?预先感谢您的建议和答案。

回答by pmg

You need to declare (not define) at least one function before using it.

在使用它之前,您需要声明(而不是定义)至少一个函数。

function2();                 /* declaration */
function1() { function2(); } /* definition */
function2() { function1(); } /* definition */

int main(void) { function1(); return 0; }

回答by Andrew White

Foward declare your functions...

转发声明你的功能......

function1();
function2();

function2(){
  function1()
}
function1 (){
  function2()
}

main () {
 function1()
}

回答by Andrew White

Try:

尝试:

function1();
function2();

function2(){
  function1()
}
function1 (){
  function2()
}

main () {
 function1()
}

回答by Jens Gustedt

Forward declare your functions, but by using prototypes. If you have a lot of them such that you can't handle this, this is the moment to think of your design and to create a .h file with all your prototypes. Use

Forward 声明您的函数,但使用原型。如果你有很多这样你无法处理这个问题,现在是考虑你的设计并创建一个包含所有原型的 .h 文件的时候了。用

int function1(void);
int function2(void);

if that was what you meant. int function1()already is different from that in C. Help the compiler such that he can help you.

如果那是你的意思。int function1()已经与 C 中的不同。帮助编译器,以便他可以帮助您。

回答by Anshu Sharma

This is how C works. We need to declare the function before use. like when you use any variable, you must have declare first then you would have use it.

这就是 C 的工作方式。我们需要在使用前声明函数。就像使用任何变量时一样,您必须先声明,然后才能使用它。

Declaration:- function1(); function2(); and then put your own code.

声明:- function1(); 函数2(); 然后放上自己的代码。