C++報錯error cannot deduce type of initializer list because initializer list was not found include

  • 完整錯誤信息: 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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章