2014-05-14 30 views
0

我知道這個主題經常出現在stackoverflow上,但這裏的情況有所不同: 以下代碼與MsVC++ 2013編譯,但不能與win32-g ++編譯。是否有任何方式使它與兩個編譯器一起工作?for_each和lambda上的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更改編譯器開關。

回答

0

事實證明,我被-sdd = C++ 11被默認啓用的聲明誤導了。 我最終找到了一種方法,使用Project> Build Options>編譯器設置 將其打開,然後單擊複選框「讓g ++遵循C++ 11 ISO C++語言設置」複選框。

對於那些誰與QtCreator工作以下行添加到項目文件: QMAKE_CXXFLAGS + = -std = C++ 11