60
#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
2

11 Answers 11

98

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

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

2
  • but you can use a class member before you declare it. So this explanation is not satisfying.
    – Walter
    Commented Nov 22, 2011 at 22:12
  • 15
    @Walter My explanation covers specifically his problem, and since he's new, there is no point flooding him with so much information.
    – Nasreddine
    Commented Nov 22, 2011 at 22:15
17

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

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.

10

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

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(); } 
2
  • 1
    There is nothing strange, all members of the class are already defined inside the body of member functions. Commented Nov 23, 2011 at 2:08
  • @Gene It's not immediately obvious (to a beginner/outsider anyway) why in namespace X { int a() { return b;} int b; } we have a problem, but not if we replace namespace by class (and add ; at the end).
    – Walter
    Commented Nov 23, 2011 at 11:08
4

You have to put the function before the main function.

3

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.

2

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

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

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.