C++ 为什么我的 HelloWorld 函数没有在这个范围内声明?

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

Why is my HelloWorld function not declared in this scope?

c++scope

提问by MatthewSot

#include <iostream>

using namespace std;

int main()
{
    HelloWorld();
    return 0;
}

void HelloWorld()
{
    cout << "Hello, World" << endl;
}

I am getting the following compilation error with g++:

我在使用 g++ 时遇到以下编译错误:

l1.cpp: In function 'int main()':
l1.cpp:5:15: error: 'HelloWorld' was not declared in this scope

回答by Mysticial

You need to either declare or define the function before you can use it. Otherwise, it doesn't know that HelloWorld()exists as a function.

您需要先声明或定义该函数,然后才能使用它。否则,它不知道HelloWorld()作为函数存在。

Add this before your main function:

在你的主函数之前添加这个:

void HelloWorld();

Alternatively, you can move the definition of HelloWorld()before your main():

或者,您可以HelloWorld()在您的之前移动 的定义main()

#include <iostream>
using namespace std;

void HelloWorld()
{
  cout << "Hello, World" << endl;
}

int main()
{
  HelloWorld();
  return 0;
}

回答by Nasreddine

You must declare the function before you can use it:

您必须先声明该函数,然后才能使用它:

#include <iostream>

using namespace std;

void HelloWorld();

int main()
{
    HelloWorld();
    return 0;
}

void HelloWorld()
{
    cout << "Hello, World" << endl;
}

or you can move the definition of HelloWorld()before main()

或者你可以移动HelloWorld()之前的定义main()

回答by matthias

You need to forward declare HelloWorld()so mainknows what it is. Like so:

您需要转发声明,HelloWorld()以便main知道它是什么。像这样:

#include <iostream>
using namespace std;
void HelloWorld();
int main()
{
  HelloWorld();
  return 0;
}
void HelloWorld()
{
  cout << "Hello, World" << endl;
}

回答by Morten Kristensen

You need to have either a prototype of the function before the invocation or the whole function before it.

您需要在调用之前拥有函数的原型,或者在调用之前拥有整个函数。

So the first is:

所以第一个是:

void HelloWorld();

int main() {
  HelloWorld();
  return 0;
}

void HelloWorld() {
  cout << "Hello, World" << endl;
}

And the second way is:

第二种方式是:

void HelloWorld() {
  cout << "Hello, World" << endl;
}

int main() {
  HelloWorld();
  return 0;
}

回答by Gene Bushuyev

There is one more possibility for some reason nobody mentioned, namely using extern declaration:

由于某种原因没有人提到,还有另一种可能性,即使用 extern 声明:

#include <iostream>
using namespace std;
int main()
{
  extern void HelloWorld();
  HelloWorld();
  return 0;
}
void HelloWorld()
{
  cout << "Hello, World" << endl;
}

It's preferable when you don't want to introduce name of the function into namespace scope.

当您不想将函数的名称引入命名空间范围时,这是可取的。

回答by Walter

All these answers are correct, but strangely enough, this would have worked:

所有这些答案都是正确的,但奇怪的是,这会奏效:

struct Hello {
  static int main() { World(); return 0; } /* note: World() not declared yet */
  static void World() { std::cout<<"Hello World"; }
};
int main() { return Hello::main(); } 

回答by Tusk

You have to put the function before the main function.

你必须把函数放在主函数之前。

回答by gprathour

in C++ you need to define or at least declare the functions before calling them.

在 C++ 中,您需要在调用函数之前定义或至少声明函数。

What you are trying to do till now is something like this :

到目前为止,您正在尝试做的是这样的:

int main()
{
   cout << b;
   int b = 10;
}

So you can also trying like this :

所以你也可以这样尝试:

#include <iostream>
using namespace std;  

void HelloWorld();

int main()  
{
    HelloWorld();
    return 0;  
}
void HelloWorld()
{
  cout << "Hello, World" << endl;  
} 

It is a good practice in C++ to define all other functions before the main function.

在 C++ 中,在 main 函数之前定义所有其他函数是一个很好的做法。

回答by moshbear

Rearrange HelloWorld()so that it appears before main():

重新排列HelloWorld()使其出现在 之前main()

#include <iostream>
using namespace std;
void HelloWorld()
{
    cout << "Hello, World" << endl;
}
int main()
{
    HelloWorld();
    return 0;
}