Why is my HelloWorld function not declared in this scope?

C++Scope

C++ Problem Overview


#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

C++ Solutions


Solution 1 - C++

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

Solution 2 - C++

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

Solution 3 - C++

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

Solution 4 - C++

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

Solution 5 - C++

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.

Solution 6 - C++

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

Solution 7 - C++

You have to put the function before the main function.

Solution 8 - C++

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.

Solution 9 - C++

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

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

Solution 10 - C++

If you're defining you're functioning below your main function you should declare it above first.

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

Solution 11 - C++

Declare void HelloWorld() above main() method or add function prototype void HelloWorld(); so compiler can compile it . if the prototyped function does not exist then you get a linker error. in this case you are trying like this

  std::cout <<hello<<endl;
  std::string hello = "hello world";

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMatthewSotView Question on Stackoverflow
Solution 1 - C++MysticialView Answer on Stackoverflow
Solution 2 - C++NasreddineView Answer on Stackoverflow
Solution 3 - C++matthiasView Answer on Stackoverflow
Solution 4 - C++Morten KristensenView Answer on Stackoverflow
Solution 5 - C++Gene BushuyevView Answer on Stackoverflow
Solution 6 - C++WalterView Answer on Stackoverflow
Solution 7 - C++TuskView Answer on Stackoverflow
Solution 8 - C++gprathourView Answer on Stackoverflow
Solution 9 - C++moshbearView Answer on Stackoverflow
Solution 10 - C++allen alexView Answer on Stackoverflow
Solution 11 - C++sameraze agvvlView Answer on Stackoverflow