Why is my HelloWorld function not declared in this

2019-01-11 11:56发布

问题:

#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++:

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

回答1:

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.

Add this before your main function:

void HelloWorld();

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

#include <iostream>
using namespace std;

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

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


回答2:

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()



回答3:

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

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


回答4:

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;
}


回答5:

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

#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.



回答6:

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(); } 


回答7:

You have to put the function before the main function.



回答8:

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

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.



回答9:

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

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


标签: c++ scope