- 完整錯誤信息: error: cannot deduce type of initializer list because std::initializer_list was not found; include
<initializer_list> - 問題發現在list上, 如:
#include <iostream>
using namespace std;
int main(){
for(auto k: {1, 2, 3})
cout<< k<< " ";
return 0;
}
- 解決方法很簡單,因爲這裏{1,2,3}用到了C++11的特性
- 不需要添加<initializer_list>到頭文件
- 故編譯的時候寫
g++ -std=c++11 c文件
即可 - 更方便的,讓其默認採用c++11特性的方法見此link