-1

I got strange error why compiling a simple source code in g++:

#include <vector>
int main(int argc, const char* argv[]){
    std::vector<int> a{1,2,3};
    return 0;
}

The compiler output is

g++ -c -Wall main.cpp
main.cpp:4:20: error: expected ';' at end of declaration
    std::vector<int> a{1,2,3};
                      ^
                      ;
1 error generated.

When I compile it in xCode everything works fine.

Jiří Lechner
  • 750
  • 6
  • 19
  • Which version of g++?, please give us something reproducible. Post a [Minimal Complete and Verifiable Example](http://stackoverflow.com/help/mcve). Nonetheless, try adding `-std=c++11` or `-std=c++0x` to your compiler flag – WhiZTiM Jul 06 '16 at 23:39
  • 1
    It's likely you aren't compiling with C++11 or higher – Tas Jul 06 '16 at 23:39
  • Does your compiler support C++11? Try adding `-std=c++11` to your compilation flags. – drglove Jul 06 '16 at 23:39

1 Answers1

4

Change your compiler command to

> g++ -std=c++11 -c -Wall main.cpp
> #   ^^^^^^^^^^

if your GCC compiler version doesn't support that flag you should upgrade to a newer version (which probably supports the current standard by default).

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • 1
    Apparently the advice in comments adding the flag -std=gnu++11 solve the problem. I was confused since -std=gnu++14 should be the default flag but it seems I have old version of g++. Thanks for advices. – Jiří Lechner Jul 06 '16 at 23:47