1

When I run this hello world function:

#include <iostream>
/*
welcome to your first c++ tutorial! Congrats lets have some fun :) 


*/
int main() {
    std::cout << "hello world\n";
    int e{ 2 };
    std::cout << e;
    return 0;
}

I get this error:

[Running] cd "/Users/tbarton/Documents/GitHub/c++ practice files/" && g++ main.cpp -o main && "/Users/tbarton/Documents/GitHub/c++ practice files/"main
main.cpp:9:10: error: expected ';' at end of declaration
    int e{ 2 };
         ^
         ;
1 error generated.

[Done] exited with code=1 in 0.388 seconds

I'm a super noob for c++ so I'm lost. Any idea what it is?

Travasaurus
  • 601
  • 1
  • 8
  • 26

1 Answers1

3

Maybe your compiler is older and not setup for c++11 (or higher). You could try:

g++ main.cpp -o main -std=c++11

See your same code working here: https://godbolt.org/z/77oc5Y

And here is the same code running -std=c++98: https://godbolt.org/z/aEszPb (with the error you are seeing)

update

since the above links might stop working in future here is the output:

Compiled with gcc 4.1.2:

<source>: In function 'int main()':
<source>:9: error: a function-definition is not allowed here before '{' token
<source>:10: error: 'e' was not declared in this scope
Compiler returned: 1

Compiled with gcc 10.2:

ASM generation compiler returned: 0
Execution build compiler returned: 0
Program returned: 0
hello world
2

Compiled with gcc 10.2 with the compiler flag -std=c++98:

<source>: In function 'int main()':
<source>:9:10: warning: extended initializer lists only available with '-std=c++11' or '-std=gnu++11'
    9 |     int e{ 2 };
      |          ^
ASM generation compiler returned: 0
<source>: In function 'int main()':
<source>:9:10: warning: extended initializer lists only available with '-std=c++11' or '-std=gnu++11'
    9 |     int e{ 2 };
      |          ^
Execution build compiler returned: 0
Program returned: 0
hello world
2
code_fodder
  • 15,263
  • 17
  • 90
  • 167
  • 1
    @user4581301 ah, I did not know that thanks - I'll post them up - although the question was closed for some reason that I can't quite fathom :o – code_fodder Sep 04 '20 at 15:45
  • 1
    The full message for a typo or not reproducible close includes, or at least used to include, something along the lines of "or the question was answered in a way will not be helpful to future askers." It's the value added by the examples you added that made this answer worth an upvote. – user4581301 Sep 04 '20 at 16:28
  • 1
    @user4581301 that's odd - sounds like the question was closed due to the answer possibly going out of date? - Updated the output to show a few variations - using the exact same code as OP posted – code_fodder Sep 04 '20 at 16:42
  • 1
    I'm one of the close-voters. I voted about the same time you answered and intended to make a "Try `g++ main.cpp -o main -std=c++11`" comment, but your answered covered it and more. Usually a question like this needs to be closed because it attracts to attract junk answers and one-liners that should have been comments. Yours is a rare one that put some thought into the answer. – user4581301 Sep 04 '20 at 16:49
  • 1
    @user4581301 - sorry I am not trying to be difficult - I am really just curious. Shouldn't the question just be protected instead of closed? - its a legit question with a perfectly good minimal example etc...? – code_fodder Sep 04 '20 at 16:51
  • 2
    No worries. Protection's up to the mods rather than a community action. There's a lot more community than mods. My view, there are a lot of perfectly legitimate questions that will get terse or garbage answers. Closing a question is not a punishment. Usually it's a defense for the question so it doesn't get buried in garbage while the question is cleaned up, clarified, or whatever it needs. Protection might work here, but how many more ways can this question be answered? No matter what you do they will all come down to "Compile the code with the correct Standard Revision." – user4581301 Sep 04 '20 at 17:00
  • 1
    @user4581301 oh ok, that makes sense.... but yeah, that reason code is a bit rubbish :) – code_fodder Sep 04 '20 at 17:28
  • I think this is the solution I'm looking for! I'm working on how to update my compiler right now and if it works I'll come back with a check mark! Thanks for the clear and concise answer! – Travasaurus Sep 04 '20 at 21:11
  • 1
    @Travasaurus you must be using a *really* old compiler - just out of curiosity what version is it and why is it so old? - is it some legacy tool chain you are working on? Just by messing around in godbolt gcc v4.1.2 fails, v4.4.7 complains but compiles ok. Note that in 4.4.7 you need to use `-std=c++0x` IIRC this is because this is a sort of pre-release for c++11 and the release date had not been settled on yet. So if you can get your compiler beyond 4.4.7 you should be ok - but you will be missing many other c++11/14/17 features... – code_fodder Sep 04 '20 at 21:22
  • when I run gcc --version i get: Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/4.2.1 Apple clang version 11.0.0 (clang-1100.0.33.17) Target: x86_64-apple-darwin19.2.0 Thread model: posix InstalledDir: /Library/Developer/CommandLineTools/usr/bin I dont know why its so old, I'm just starting to learn c++ from an R/python past – Travasaurus Sep 04 '20 at 21:54
  • 1
    @Travasaurus ah you are on a Mac - I don't know the specifics of how gcc is installed on the Mac (via XCode?) but this looks like it uses clang under the hood. Based on this: https://stackoverflow.com/questions/20410587/how-to-find-gcc-version-on-mac it looks like you get clang by default and you need to install gcc specifically - or some such! - bit beyond my knowledge... – code_fodder Sep 04 '20 at 22:06