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

猜你喜欢

转载自blog.csdn.net/Xurui_Luo/article/details/106757233