【问题标题】:win32-g++ error on for_each and lambdafor_each 和 lambda 上的 win32-g++ 错误
【发布时间】:2014-05-14 09:18:41
【问题描述】:

我知道这个主题经常出现在 stackoverflow 上,但这里的情况不同: 以下代码使用 MsVC++ 2013 编译,但不能使用 win32-g++ 编译。有没有办法让它在两个编译器上都工作?

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;



int main()
{
    vector<int>my_vector;
    for(int i=0; i < 6; i++)
        my_vector.push_back(i);
    for_each( my_vector.begin(), my_vector.end(), [ ](int  n){cout<<n;});
    return 0;
}

报告的错误是

||=== stdtest, Debug ===|
D:\dev\CplusPlus\stdtest\main.cpp||In function 'int main()':|
D:\dev\CplusPlus\stdtest\main.cpp|13|warning: lambda expressions only available with -std=c++11 or -std=gnu++11 [enabled by default]|
D:\dev\CplusPlus\stdtest\main.cpp|13|error: no matching function for call to 'for_each(std::vector<int>::iterator, std::vector<int>::iterator, main()::<lambda(int)>)'|
D:\dev\CplusPlus\stdtest\main.cpp|13|note: candidate is:|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\bits\stl_algo.h|4436|note: template<class _IIter, class _Funct> _Funct std::for_each(_IIter, _IIter, _Funct)|
D:\dev\CplusPlus\stdtest\main.cpp|13|error: template argument for 'template<class _IIter, class _Funct> _Funct std::for_each(_IIter, _IIter, _Funct)' uses local type 'main()::<lambda(int)>'|
D:\dev\CplusPlus\stdtest\main.cpp|13|error:   trying to instantiate 'template<class _IIter, class _Funct> _Funct std::for_each(_IIter, _IIter, _Funct)'|
||=== Build finished: 3 errors, 1 warnings (0 minutes, 0 seconds) ===|

我应该补充一点,我假设 C++11 标准已打开,因为“默认启用”。万一我错了,我很难找出如何从 Code::Blocks 更改编译器开关。

【问题讨论】:

    标签: foreach lambda g++


    【解决方案1】:

    原来我被 -sdd=C++11 是“默认启用”的说法误导了。 我最终找到了一种使用 Project > Build Options> compiler settings 打开它的方法 然后我点击了“让 g++ 遵循 c++11 ISO C++ 语言设置”复选框。

    对于那些正在使用 QtCreator 的人,将以下行添加到项目文件中: QMAKE_CXXFLAGS += -std=c++11

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-10-06
      • 1970-01-01
      • 2020-04-05
      • 2015-03-27
      • 1970-01-01
      • 1970-01-01
      • 2017-07-28
      相关资源
      最近更新 更多