Why can a struct defined inside a function not be used as functor to std::for_each?
Posted
by
Oswald
on Stack Overflow
See other posts from Stack Overflow
or by Oswald
Published on 2010-12-29T01:47:37Z
Indexed on
2010/12/29
1:54 UTC
Read the original article
Hit count: 508
c++
The following code won't compile. The compiler complains about *no matching function for call to for_each*. Why is this so?
#include <map>
#include <algorithm>
struct Element
{
void flip() {}
};
void flip_all(std::map<Element*, Element*> input)
{
struct FlipFunctor
{
void operator() (std::pair<Element* const, Element*>& item)
{
item.second->flip();
}
};
std::for_each(input.begin(), input.end(), FlipFunctor());
}
When I move struct FlipFunctor
before function flip_all
, the code compiles.
Full error message:
no matching function for call to ‘for_each(std::_Rb_tree_iterator<std::pair<Element* const, Element*> >, std::_Rb_tree_iterator<std::pair<Element* const, Element*> >, flip_all(std::map<Element*, Element*, std::less<Element*>, std::allocator<std::pair<Element* const, Element*> > >)::FlipFunctor)’
© Stack Overflow or respective owner