Problems in using C++11

I had been using Cygwin + NetBeans till now and today I found out that I was using an outdated version of GCC (3.4.4) so I updated to GCC 4.7.2 by installing MinGW. After doing that, I wrote this piece of code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <vector>

using std::vector;

int main()
{
    vector<int> v{1,2,3,4,5,6,7,8,9};
    for(auto &i : v)
        i*=i;
    for(auto i : v)
        std::cout << i << " ";
    std::cout << '\n';
    
    return 0;
}  


I get loads of errors in this program. Some of them are:

warning: extended initializer lists only available with -std=c++11 or -std=gnu++11 [enabled by default]

error: in C++98 'v' must be initialized by constructor, not by '{...}'

from UseVector.cpp:2:

*c:\mingw\bin../lib/gcc/mingw32/4.7.2/include/c++/bits/stl_vector.h:387:9: note: template std::vector::vector(_InputIterator, _InputIterator, const allocator_type&) c:\mingw\bin../lib/gcc/mingw32/4.7.2/include/c++/bits/stl_vector.h:387:9: note: template argument deduction/substitution failed: UseVector.cpp:8:36: note: cannot convert '3' (type 'int') to type 'const allocator_type& {aka const std::allocator&}'


I am also getting errors in the included header files. I have enabled NetBeans to use MinGW instead of Cygwin and I have set the Path to C:/MinGW/bin. Any ideas about what the problem might be ?
Add -std=c++11 to the compiler options, or add it as an argument:
g++ -std=c++11 prog.cpp -o prog.exe


Your code is fine from what I can see. The reason why it doesn't compile is because the compiler is in its default C++98 mode.
Thanks a lot ! :)
Topic archived. No new replies allowed.