无法从括号括起来的初始化程序列表转换为std :: vector [英] Could not convert from brace-enclosed initializer list to std::vector
问题描述
我看到了很多类似的问题,但我不认为我看到了相同的问题。这是很基本的。我的讲师的一些代码未能编译,我把问题提炼到这个测试用例:
void foo(vector< int> ; v){
}
void fooUsage(){
foo({0,1,2});
}
此操作失败:
无法将'{0,1,2}'从'<括号括起始化程序列表>'转换为'std :: vector< int>
p>
注意:它适用于GCC 5.0.0 20141228,但在我的GCC 4.7.1(tdm-1)上失败。
对不起,如果这太基础,但我不知道C ++ 11很好。
原来,我只需要在gcc命令行中添加 -std = c ++ 11
。
注意:我错误地认为这是默认开启,因为我也得到一些警告,像这样:
扩展初始化列表可以使用-std = c ++ 11或-std = gnu ++ 11 [默认启用]
默认?这是误导我的。
I saw a lot of similar questions, but I don't think I saw quite the same one. It's pretty basic. Some code from my lecturer is failing to compile, and I distilled the problem to this test case:
void foo(vector<int> v) {
}
void fooUsage() {
foo({0, 1, 2});
}
This fails with:
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 fails on my GCC 4.7.1 (tdm-1).
Sorry if this is too basic but I don't know C++11 very well.
It turns out I only needed to add -std=c++11
to the gcc command line.
Note: I was mistakenly thinking that this is on by default, since I was also getting 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"? That's what was misleading me.
这篇关于无法从括号括起来的初始化程序列表转换为std :: vector的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!