Problem getting g++ up to date

I'm trying to update g++ to 4.8, with C++ 11 support, on Ubuntu, but trying to initialize a vector with an initializer list still gives a compiler error.

I've run
sudo apt-get install g++ 4.8
but to no avail.

code:
vector<int> v = {0, 2, 5 11};

output:
error: in C++98 'v' must be initialized by constructor, not by '{...}'
error: could not convert '{0, 2, 5, 11}' from '<brace-enclosed initialzer list>' to 'std::vector<int>'
Last edited on
I just got this working today as well...

You need to add ¨-std=c++0x¨ to the command line when building.

Downloaded the Code::blocks IDE today and downloaded c++ 4.8 in the same manner that you used. To get the C++11 support, I had to right-click on the project, select ¨build options¨, then re-select GNU GCC Compiler, then in ¨Compiler Flags¨ window I checked ¨Have g++ follow the coming c++0x ISO C++ language standard [-std=c++0x]¨

For my first test, I compiled this:
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <vector>

int main(int argc, char* argv[])
{
    double a = 1.0;
    auto b = a;
    std::cout << b << ' ';

    std::vector<int> v = { 0, 2, 5, 11};
    for (int i : v)
        std::cout << i << ' ';
}
1 0 2 5 11
Process returned 0 (0x0)    excecution time : 0.002s
Press ENTER to continue.
Last edited on
First check gcc --version is 4.8 (if you havent already) then use -std=c++11 instead of -std=c++0x. -std=c++11 was for proposed features of C++0x that were implemented in gcc early, not sure if -std=c++11 and -std=c++0x are exactly the same
-std=c++11 and -std=c++0x is exactly the same.
Topic archived. No new replies allowed.