Why is my HelloWorld function not declared in this area?

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

I get the following compilation error with g ++:

 l1.cpp: In function 'int main()': l1.cpp:5:15: error: 'HelloWorld' was not declared in this scope 
+45
c ++ scope
Nov 22 2018-11-21T00:
source share
9 answers

You need to either declare or define a function before you can use it. Otherwise, he does not know that HelloWorld() exists as a function.

Add this before your main function:

 void HelloWorld(); 

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

 #include <iostream> using namespace std; void HelloWorld() { cout << "Hello, World" << endl; } int main() { HelloWorld(); return 0; } 
+81
Nov 22 2018-11-22T00:
source share

You must declare this 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() to main()

+19
Nov 22 2018-11-22T00:
source share

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

 #include <iostream> using namespace std; void HelloWorld(); int main() { HelloWorld(); return 0; } void HelloWorld() { cout << "Hello, World" << endl; } 
+15
Nov 22 2018-11-22T00:
source share

Before that, you need either a function prototype before calling, or an entire function.

So the first one:

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

And the second way:

 void HelloWorld() { cout << "Hello, World" << endl; } int main() { HelloWorld(); return 0; } 
+9
Nov 22 2018-11-22T00:
source share

There is another possibility, for some reason no one has mentioned, namely the use of the extern expression:

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

This is preferable if you do not want to enter the name of the function in the namespace area.

+8
Nov 23 '11 at 2:13
source share

All of these answers are correct, but, oddly enough, this would work:

 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(); } 
+5
Nov 22 '11 at 10:16
source share

You must put the function before the main function.

+4
Nov 22 2018-11-22T00:
source share

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

What you are trying to do so far looks something like this:

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

So you can also try:

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

Good practice in C ++ to define all other functions before the main function.

+3
Nov 23
source share

We reorder HelloWorld() so that it appears before main() :

 #include <iostream> using namespace std; void HelloWorld() { cout << "Hello, World" << endl; } int main() { HelloWorld(); return 0; } 
+2
Nov 22 2018-11-21T00:
source share



All Articles