problem with for_each and lambda expressions

May 13, 2014 at 6:15pm
Hello,
I'm learning STL and lambdas for the very latest version of C++.
I was hoping that in the following program I would fill a vector with values 1 to 6 and then to print them out. But the compiler complains that for_each is not recognized!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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){ std::cout<<n;});
    return 0;
}
May 13, 2014 at 6:57pm
What compiler are you using?
May 13, 2014 at 7:08pm
I'm using g++ in MinGW.
It's the one that ships with QtCreator 5.2 and the CodeBlocks I just downloaded.
Last edited on May 13, 2014 at 7:09pm
May 13, 2014 at 7:39pm
I tried my program with Visual Studio express 2013. There were no problems and the program printed my sequence 012345 as expected.
May 13, 2014 at 7:41pm
Hmm.. I have no idea what is wrong.

Is Qt overloading "for_each" in some way?

Are you sure your compiler options are set up properly?

I'm sorry I can't offer any more useful help here...
May 13, 2014 at 7:42pm
Can you post the exact error message?
May 13, 2014 at 8:29pm
1
2
3
4
5
6
7
8
9
||=== 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) ===|
May 13, 2014 at 8:40pm
Turn on C++11 by passing -std=c++11 to the compiler and see if that helps.
May 14, 2014 at 9:08am
Ok, that's going to be hard, because I am using either CodeBlocks ot QTCreator. But I must point out that -std=c++11 is "enabled by default" as the above messages indicate
May 14, 2014 at 9:12am
It is not c++11 mode that is enabled by default, but warning.
In C::B setting is under settings→compiler. I do not familiar with QT creator, but any sane IDE should at least allow to add your own compiler parameters.
May 14, 2014 at 9:43am
OK, it turns out that I found after much searching that I could turn on
-std=c++11
and this fixes the problem. But I was misled by the message that
led me to believe this was turned on by default.
May 14, 2014 at 9:55am
It is understandable that the message could be confusing. What it normally shows between the brackets is the warning flag that was used to enable the warning.
http://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html

But I guess some warnings are not controlled by flags but are enabled by default.
May 14, 2014 at 11:12pm
The C++11 mode is not enabled by default -- lambda expressions are enabled by default when you select C++11 mode.

It is a poorly-worded message.
Topic archived. No new replies allowed.