1
votes

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.

2
Are you trying compile the code inside vim?Vitor Villar
Be careful to distinguish C++ and C. C99 is a standard for C; there is no C++99.echristopherson
You should be using the command g++ -std=c++11. gcc is for c files and g++ is for c++ files.jodag
Also, what type is v. You probably want for (char c : v ) cout << c; if v is some std container (like std::vector)jodag

2 Answers

3
votes

As mentioned in the previous answer, you are attempting to use a feature of the latest C++ standard (called C++11) while compiling for older standard. C++11 is the latest C++ standard and the only one supporting range based for.

Now you need to distinguish between the C++ standard and the compiler support for that standard. Along the past few years, support for C++11 features was gradually added to the gcc compiler. The following link shows which C++11 feature is supported by which gcc version:

C++0x/C++11 Support in GCC

As you can see, range based for was added in gcc 4.6, so you do not need gcc 4.8 in order to use this feature - gcc 4.6 or later will suffice.

When compiling, you will also need to tell the compiler which standard to compile against. The -std=whatever tells the compiler which standard to use.

You are currently using "-std=c99", telling the compiler to compile using an old C++ standard. Instead you need to set this flag to c++11. If this doesn't work on the gcc version you are using, try using "-std=c++0x" instead (C++0x is an old name of the C++11 standard.)

5
votes

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.