0

I am at the absolutely newest level of new when it comes to C++. It may seem like a noob mistake, but I think I'm missing something with my first program, "Hello World!".

I'm running from Ubuntu (not sure if this is any different from working with Windows), and I'm using a book called Teach Yourself C++ in 21 Days.

The code I'm resembling looks exactly like this:

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

I have this exactly in my text editor, but I keep getting greeted by the same error whenever I try to compile it!

first.cpp:2:22: fatal error: iostream.h: No such file or directory compilation terminated.

I'm pretty distressed as this is literally the first step in my coding career! I'm not sure if ubuntu needs to be treated differently than Windows (which is what the book is using as reference).

Help!

Thomas Boudreau
  • 1
  • 1
  • 1
  • 1
  • 2
    `#include ` – aaronman Jul 13 '13 at 20:18
  • 2
    `` isn't a proper thing; use ``. – Oliver Charlesworth Jul 13 '13 at 20:18
  • 3
    You might want to use a better book: [The Definitive C++ Book Guide and List](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – Blastfurnace Jul 13 '13 at 20:26
  • 2
    You have a very old edition of "Teach Yourself C++ in 21 Days". The newest edition doesn't make this mistake (or the mistake of leaving off the `std` qualifier from `cout`). Note that these problems probably were not mistakes when the book you have was published due to the fact that the language changed in many details when it was standardized. You should get the newer edition or consider Blastfurnace's suggestion. – Michael Burr Jul 13 '13 at 20:53
  • Possible duplicate of [Why can't g++ find iostream.h?](https://stackoverflow.com/questions/13103108/why-cant-g-find-iostream-h) – phuclv Aug 12 '18 at 11:31

5 Answers5

7

There are two problems here:

You need to omit the .h suffix:

#include <iostream>

Also, cout is an unqualified name, and needs to be qualified with the std namespace since you are not using namespace std:

std::cout << "Hello World!\n";
ApproachingDarknessFish
  • 14,133
  • 7
  • 40
  • 79
cdhowie
  • 158,093
  • 24
  • 286
  • 300
2

There shouldn't be any iostream.h it's simply called iostream and should look like this:

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

(also notice the std:: before the cout, since it means that it's from the standard namespace.)

AliciaBytes
  • 7,300
  • 6
  • 36
  • 47
0

You want just iostream

#include <iostream>

I suspect the book is very old. Names are qualified in the std namespace, so you may want to add

using namespace std;

For now at least.

Jeff Foster
  • 43,770
  • 11
  • 86
  • 103
0

use

#include <iostream>

in general STL header file don't have a .h

alexbuisson
  • 7,699
  • 3
  • 31
  • 44
  • No standard C++ headers have an extension. By that, I exclude the C ones that are included in C++. – chris Jul 13 '13 at 21:27
-2

you need to compile using g++ compiler, not by gcc

g++ hello.cpp