简体   繁体   English

无法从大括号括起来的初始化列表转换为std :: vector

[英]Could not convert from brace-enclosed initializer list to std::vector

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). 注意:它适用于GCC 5.0.0 20141228但在我的GCC 4.7.1(tdm-1)上失败。

Sorry if this is too basic but I don't know C++11 very well. 对不起,如果这太基础但我不太了解C ++ 11。

It turns out I only needed to add -std=c++11 to the gcc command line. 事实证明我只需要将-std=c++11添加到gcc命令行。

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. 这就是误导我的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法从大括号括起来的初始化列表转换为std元组 - Could not convert from brace-enclosed initializer list to std tuple 无法从 '<brace-enclosed initializer list> ' 为向量</brace-enclosed> - Could not convert from ‘<brace-enclosed initializer list>’ to vector 错误:无法转换&#39; <brace-enclosed initializer list> ()&#39;从&#39; <brace-enclosed initializer list> &#39;到&#39;结构&#39; - Error: could not convert '<brace-enclosed initializer list>()' from '<brace-enclosed initializer list>' to 'struct' 无法转换&#39; <brace-enclosed initializer list> 至 - Could not convert from '<brace-enclosed initializer list> to 无法将…转换为&#39; <brace-enclosed initializer list> 到地图 - could not convert … from '<brace-enclosed initializer list>' to map 无法从转换 <brace-enclosed initializer list> 到int(*)数组 - Could not convert from <brace-enclosed initializer list> to int(*) array 无法从转换<brace-enclosed initializer list> - could not convert from <brace-enclosed initializer list> 无法从 '<brace-enclosed initializer list> ' 到 map</brace-enclosed> - could not convert from ‘<brace-enclosed initializer list>’ to map 无法转换{...} <brace-enclosed initializer list> 结构 - could not convert {…} from <brace-enclosed initializer list> to struct 无法从大括号括起来的初始化列表转换 - Could not convert from brace-enclosed initializer list
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM