C++ 为什么函数不能在 Main 之后

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

Why can't a function go after Main

c++visual-studio-2010

提问by GPPK

Why can't I put a function after main, visual studio cannot build the program. Is this a C++ quirk or a Visual Studio quirk?

为什么我不能在main之后放一个函数,visual studio无法构建程序。这是 C++ 的怪癖还是 Visual Studio 的怪癖?

eg.

例如。

int main()
{
   myFunction()
}

myFunction(){}

will produce an error that main cannot use myFunction

会产生 main 不能使用 myFunction 的错误

回答by juanchopanza

You can, but you have to declare it beforehand:

你可以,但你必须事先声明:

void myFunction(); // declaration

int main()
{
   myFunction();
}

void myFunction(){} // definition

Note that a function needs a return type. If the function does not return anything, that type must be void.

请注意,函数需要返回类型。如果该函数不返回任何内容,则该类型必须是void.

回答by Nawaz

You cannot use a name/symbolwhich is notyet declared. That is the whole reason.

您不能使用一个名称/符号这是不是还没有宣布。这就是全部原因。

It is like this:

它是这样的:

i = 10;  //i not yet declared

int i;

That is wrongtoo, exactly for the same reason. The compiler doesn't know what iis– it doesn't really care what it willbe.

出于同样的原因,这也是错误的。编译器不知道什么i-它并没有真正关心它是什么是。

Just like you write this (which also makes sense to you as well as the compiler):

就像你这样写(这对你和编译器也有意义):

int i;  //declaration (and definition too!)

i = 10;  //use

you've to write this:

你必须这样写:

void myFunction(); //declaration!

int main()
{
   myFunction() //use
}

void myFunction(){}  //definition

Hope that helps.

希望有帮助。

回答by V. K. Jayendra Varma

most of the computer programming languages have top to down approach which means code is compiled from the top. When we define a function after main function and use it in the main [myFunction () ], compiler thinks " what is this. I never saw this before " and it generates an error saying myFunction not declared. If you want to use in this way you should give a prototype for that function before you define main function. But some compilers accept without a prototype.

大多数计算机编程语言都有自顶向下的方法,这意味着代码是从顶部编译的。当我们在 main 函数之后定义一个函数并在 main [myFunction () ] 中使用它时,编译器会认为“这是什么。我以前从未见过这个”并产生一个错误,说 myFunction 未声明。如果你想以这种方式使用,你应该在定义 main 函数之前给出该函数的原型。但是有些编译器接受没有原型。

#include<stdio.h>
void myFunction(); //prototype
int main()
{
   myFunction(); //use 
}

myFunction(){ //definition 
.......;
}

回答by fiscblog

Because

因为

myFunction()

has to be declared before using it. This is c++ behaviour in general.

必须在使用前声明。这通常是 C++ 行为。

回答by Barmar

Functions need to be declared before they can be used:

函数在使用前需要声明:

void myFunction();

int main() {
  myFunction();
}

void myFunction() {
  ...
}

回答by nio

you have to forward declare a function so main can know that there is some.

你必须向前声明一个函数,这样 main 才能知道有一些。

void myFunction();

int main()
{
   myFunction();
}

void myFunction(){}

Don't forget about putting ;after each command.

不要忘记;在每个命令之后放置。

回答by vinod

specify the function declaration before calling function .So that compiler will know about return type and signature

在调用函数之前指定函数声明。这样编译器就会知道返回类型和签名

回答by alexbuisson

You have to declare the function before.

您必须先声明该函数。

void myFunction();

int main()
{
    myFunction()
 }

 myFunction(){}