C++ – Can’t compile C++ file.cpp. C++98 mode

cc++98gccUbuntuvim

I'm new to C++. When I write

for (char* c : v)
{
    cout << c;
}

I get

"range-based ‘for’ loops are not allowed in C++98 mode"

As far as I understand, I have to change my GCC version (or just mode?). My g++ -v:

Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/lib/gcc/i686-linux-gnu/4.7/lto-wrapper
Target: i686-linux-gnu
Configured with: ../src/configure -v –with-pkgversion='Ubuntu/Linaro 4.7.2-2ubuntu1' –with-bugurl=file:///usr/share/doc/gcc-4.7/README.Bugs –enable-languages=c,c++,go,fortran,objc,obj-c++ –prefix=/usr –program-suffix=-4.7 –enable-shared –enable-linker-build-id –with-system-zlib –libexecdir=/usr/lib –without-included-gettext –enable-threads=posix –with-gxx-include-dir=/usr/include/c++/4.7 –libdir=/usr/lib –enable-nls –with-sysroot=/ –enable-clocale=gnu –enable-libstdcxx-debug –enable-libstdcxx-time=yes –enable-gnu-unique-object –enable-plugin –enable-objc-gc –enable-targets=all –disable-werror –with-arch-32=i686 –with-tune=generic –enable-checking=release –build=i686-linux-gnu –host=i686-linux-gnu –target=i686-linux-gnu
Thread model: posix
gcc version 4.7.2 (Ubuntu/Linaro 4.7.2-2ubuntu1) `

Can't compile from vim (with c.vim plugin), sublime text3 and from terminal using gcc program.cpp and gcc -pedantic -std=c99 program.cpp.

I've downloaded gcc 4.8.1 but It's not deb package so can't install it properly. Also heard about C++11, C++14, C++98, C++99. Where/how can I get/install the latest version?

Solution for vim plugin c.vim:

Edit "c.vim/plugin/c.vim". Change this line

let s:C_CplusCFlags = '-Wall -g -O0 -c -std=c++11' " C++ compiler flags: compile, don't optimize

I've added "-std=c++11" and it works.

Best Answer

The range based for loop is part of C++11, so you will need to use -std=c++11 with G++ to enable the C++11 features.

Related Topic