Hello World in C ++ does not compile

3

I'm reading the book "Training in C ++ Language", but this teaching everything error. The hello world of the book simply does not compile:

#include <iostream.h>
void main() {
cout << "Primeiro Programa";
}

Follow the picture

    
asked by anonymous 11.12.2014 / 23:24

1 answer

5

If the book is really bad, throw it away and look for a better one.

You have some problems in this code

  • In C ++ the include files do not have the suffix .h as in C. It is the statement of cout .
  • Function definitions must have parentheses after their name.
  • The return of the main() function must be an integer.
  • cout needs your full name, that is, std::cout .

So it compiles:

#include <iostream>

int main() {
    std::cout << "Primeiro Programa";
}

See running on ideone . And no Coding Ground . Also I placed GitHub for future reference .

A variation used when the program starts to become more complex (not that it compensates in this case, only to show something very used):

#include <iostream>
using std;

int main() {
    cout << "Primeiro Programa";
}
    
11.12.2014 / 23:34