How to compile C++ source code ("iostream.h not found" error)?

I do not want to discuss about C++ or any programming language!I just want to know what am i doing wrong with linux ubuntu about compiling helloworld.cpp!

I am learning C++ so my steps are:

open hello.cpp in vim and write this

#include <iostream.h>
int main()
{
    cout << "Hello World!\n";`
    return 0;
}

So, after that i tried in the terminal this

g++ hello.cpp

AND the output is

hello.cpp:1:22: fatal error: iostream.h: No such file or directory
compilation terminated.

What do you suggest? Any useful step by step guide for me?Thanks!


Solution 1:

You should use #include <iostream>, not iostream.h; the .h form is very old and deprecated since years.

You can read more than you probably want to know on the .h vs non-.h forms here: http://members.gamedev.net/sicrane/articles/iostream.html

(Plus, you should write std::cout or have a line using namespace std; otherwise your next error will be about the compiler not finding a definition for cout.)

Solution 2:

You should change iostream.h to iostream. I was also getting the same error as you are getting, but when I changed iostream.h to just iostream, it worked properly. Maybe it would work for you as well.

In other words, change the line that says:

#include <iostream.h>

Make it say this instead:

#include <iostream>

The C++ standard library header files, as defined in the standard, do not have .h extensions.

As mentioned Riccardo Murri's answer, you will also need to call cout by its fully qualified name std::cout, or have one of these two lines (preferably below your #include directives but above your other code):

using namespace std;
using std::cout;

The second way is considered preferable, especially for serious programming projects, since it only affects std::cout, rather than bringing in all the names in the std namespace (some of which might potentially interfere with names used in your program).