2

I'm using atom to practice C++ (I'm very new). I just learned to initialize variables like the following:

#include <iostream>

using namespace std;

int main() {

  int myInt {};
  
  return 0;
}

When I build and run the previous code in codelite I receive no errors. However, if I compile my atom file dailyPractice10.cpp using my MacBook terminal (zsh) I get the following error:

dailyPractice10.cpp:7:12: error: expected ';' at end of declaration
int myInt {};
        ^
        ;
1 error generated.

I'm using the following command to compile it on terminal:

g++ -o dailyPractice10 dailyPractice10.cpp (compiles)

./dailyPractice10 (runs program)

Does anyone have any feedback why this code runs in codelite but doesn't compile in terminal?

drescherjm
  • 10,365
  • 5
  • 44
  • 64
A.V.S.
  • 27
  • 8
  • 1
    Is c++11 enabled? – drescherjm Jun 20 '20 at 16:04
  • BTW there is a very recent question with the same problem on macOS but using Visual Studio Code: [https://stackoverflow.com/questions/62487546/c-compile-error-expected-at-end-of-declaration-when-using-direct-brace-i](https://stackoverflow.com/questions/62487546/c-compile-error-expected-at-end-of-declaration-when-using-direct-brace-i) – drescherjm Jun 20 '20 at 16:06
  • When I type gcc --version into terminal it states: "...Apple clang version 11.0.3..." – A.V.S. Jun 20 '20 at 16:20
  • You need to tell your compiler that it isn't in 2003 C++ mode by supplying the appropriate compiler options (probably `-std=c++17`). – PaulMcKenzie Jun 20 '20 at 16:22

3 Answers3

5

Because this feature is added from c++11.

if you will like to try below command.it will work.

$ g++ -std=c++0x -o dailyPractice10 dailyPractice10.cpp
A.V.S.
  • 27
  • 8
Nilesh Solanki
  • 336
  • 4
  • 19
0

The key to fixing this issue is to set the C++11 (or above) standards while building your code. In the console tab of the IDE, the following output is generated before the error. Notice that no standard is being defined while building the code:

make all 
Building file: ../1.cpp
Invoking: GCC C++ Compiler
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"1.d" -MT"1.o" -o "1.o" "../1.cpp"

We need to add the --std=c++1x flag to the g++ command. The following solution is for the ones using the Eclipse IDE and the MacOSX C++ compiler:

  1. Right click on the project from the "Project Explorer".
  2. Go to Properties > C/C++ Build > Settings.
  3. Under the "Tool Settings" tab, find "GCC C++ Compiler" > "Miscellaneous"

In the "Other Flags" text box, edit the text such that it looks like:

-std=c++17 -c -fmessage-length=0

If you intend to use any other c++ standard, replace "c++17" with the standard of your choice ( eg. c++20).

Apply Changes. Run Clean, and the Build again.

Neelotpal Nag
  • 181
  • 2
  • 7
0

you should try this to compile the Code

g++ -std=c++20 -o dailyPractice10 dailyPractice10.cpp
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 14 '23 at 08:16