13

I am new to programming C++ and am trying to learn myself through websites (learncpp.com) although I am already stuck on compiling my first program =( . They use Visual Studio to program their code and because I am using a macbook, I just use vi and terminal (or should I use something else?)

Here's the helloworld.cpp program I wrote based on the tutorial:

#include "stdafx.h"
#include <iostream>
{
     std::cout <<"Hello World!" <<std::end1;
     return 0;
}

when I compiled (gcc -Wall hello.cpp) I get the error :

helloworld.cpp:1:10: fatal error: 'stdafx.h' file not found

#include "stdafx.h"
         ^
1 error generated.

Can anyone give me insight on to how to fix this?

Ashley Medway
  • 7,151
  • 7
  • 49
  • 71
JackieZ3895
  • 141
  • 1
  • 1
  • 3
  • 3
    Did you try, you know, removing the line with the error? – this Mar 24 '14 at 22:30
  • 1
    Remove `#include "stdafx.h"`. – haccks Mar 24 '14 at 22:32
  • 3
    You don't need that file, just remove it. It's for a `precompiled header` that's specific to Visual Studio, which is a concept you'll cover later on. Welcome to SO! – OMGtechy Mar 24 '14 at 22:32
  • yea I did, and I got :helloworld.cpp:4:35: error: no member named 'end1' in namespace 'std' std::cout <<"Hello World!" < – JackieZ3895 Mar 24 '14 at 22:33
  • 12
    @self This isn't really a good suggestion for a beginner- if you teach them to remove every line that causes issues, they will end up with a blank document in no time. – Blue Ice Mar 24 '14 at 22:33
  • 1
    @Jackie Use `endl` (E-N-D-L), not `end1` (E-N-D-one) – Blue Ice Mar 24 '14 at 22:34
  • oh, got it, THANK YOU GUYS :D Blah, I'm so bad at this lol – JackieZ3895 Mar 24 '14 at 22:36
  • @BlueIce On the contrary, learning by trial and error is a great way to learn programming, especially if you have time to do so. – this Mar 24 '14 at 22:36
  • Trial and error might be suitable for Python, but not really for C++. Learning from a good book will save you so much time. – M.M Mar 25 '14 at 06:20
  • Your code is C++, not C. If you don't use C language, please don't add C language tag. – Gerhardh Sep 18 '18 at 08:44

3 Answers3

13
  1. stdafx.h is the precompiled header used by Visual Studio, you do not need this.
  2. You seem to have missed out the int main() function
  3. It is std::endl not std::end1

So something like this:

#include <iostream>
int main() {
     std::cout << "Hello World!" << std::endl;
     return 0;
}
Chris Drew
  • 14,926
  • 3
  • 34
  • 54
7

stdafx.h is a Precompiled Header file and it is specific to the Visual Studio. Precompiled Header file is worthless unless you are facing slow compilation Time. In your program, you don't need them at all, so you can remove that and everything will be fine.

You might be guessing if it is not needed then why we include them?

I will Explain it: Whenever we add header files (#include), The compiler will walk through it, Examine it and then compile the header file whenever CPP file is compiled.

This process is repeated for each and every CPP file that has header file included.

In case if you have 1000 CPP files in a project which has to say xyz.h header file included then the compiler will compile xyz.h file 1000 times. it may take a noticeable time.

To avoid that compiler gives us the option to "precompile" the header file so it will get compiled only once to speed up the compilation time.

Jouby
  • 2,196
  • 2
  • 21
  • 33
Jayesh Baviskar
  • 295
  • 4
  • 3
1

Two problems: a) stdafx.h is not needed (as others noted). b) 'end1' should be 'endl' (note the letter 'l' vs. the number '1').

Andy
  • 1,663
  • 10
  • 17