Hello World in C++ does not compile

Asked

Viewed 183 times

3

I am reading the book "C++ Language Training", but this teaching all wrong. The hello world of the book simply does not compile:

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

Follow picture Cópia da página do livro com o exemplo

1 answer

7


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

There are some problems in this code

  • In C++ include files do not carry the suffix .h as in C. Therein lies the statement of cout (the files are usually .hpp but import in the code does not use the extension).
  • Function definitions must have parentheses after their name.
  • The return of function main() must be a whole.
  • The cout need your full name, ie, std::cout.

Thus compiles:

#include <iostream>

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

Behold working in the ideone. And in the repl it.. Also put on the 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";
}
  • I’m reading the pdf version.

  • 5

    Then you don’t have to throw it away, just delete :P

  • I’ll do just that, garbage book!!

  • 1

    http://www.pontov.com.br/files/cpp/roadmapcpp/ApostilaProgrampCppv045.pdf. Staff also like Deitel’s "C++ How to Program" (this one has to buy).

  • I see from the workbook, it looks good...

  • 1

    Incredibly this is a variation of the first example of the book How not to program in C++. If you’re curious, this code goes against standards, but it looks like several things that compile (or at least compile) into a series of compilers (bad).

  • @Leonardo I don’t know, but it might not be bad, just too old, it’s pre-standard code, a time in the history of C++ has already been valid. Particularly not recommend Deitel, too giant, curls too much, not so modern. C++ Primer for beginner is good, Effective Modern C++ to be more on the inside. The Stroustrup, creator of language, are also updated and good, he has books that cover all language, as aimed at beginners also.

  • It Has A Detail It Doesn’t Have to Be int main(), No Visual C++ 2013 Quiet Wheel Like Void I Hate It That Put

  • http://answall.com/a/43252/101

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.