Failed to convert from a list of initializers enclosed in curly braces to std :: vector

I have seen many similar questions, but I do not think I have seen the same thing. It is pretty simple. Some of my teacher’s code did not compile, and I tried the problem in this case:

void foo(vector<int> v) { } void fooUsage() { foo({0, 1, 2}); } 

This fails:

could not convert '{0, 1, 2}' from '<brace-enclosed initializer list>' to 'std::vector<int>

Note. It works on GCC 5.0.0 20141228, but it crashes on my GCC 4.7.1 (tdm-1).

Sorry if this is too easy, but I don't know C ++ 11 very well.

+7
c ++ gcc c ++ 11
source share
2 answers

Turns out I need to add -std=c++11 to the gcc command line.

Note. I mistakenly thought this was turned on by default, as I also had some warnings like this:

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

See how it says "enabled by default"? This was misleading to me.

+2
source share
+6
source share

All Articles