how to use STL compose1(operator(), select2nd)?

Started by
3 comments, last by sprime 18 years, 8 months ago
I got an error while compiling the following code in gcc3.3.2, what's matter? //source comp.cpp //////////////////////////////////////////////// 01 #include <iostream> 02 #include <map> 03 #include <ext/functional> 04 using namespace std; 05 using namespace __gnu_cxx; 06 07 class CPrint 08 { 09 public: 10 CPrint(){}; 11 void operator()(int tvalue) 12 { 13 cout<< tvalue<<endl; 14 } 15 }; 16 17 int main() 18 { 19 map<int, int> mtest; 20 mtest[1]=2; 21 mtest[3]=4; 22 for_each(mtest.begin(), mtest.end(), 23 compose1(CPrint(), select2nd<map<int, int>::value_type>())); //the error line 24 } //////////////////////////////////////////////////////////////// //error msg://////////////////////////////////////////////////// comp.cpp: In instantiation of `__gnu_cxx::unary_compose<CPrint, __gnu_cxx::select2nd<std::pair<const int, int> > >': comp.cpp:23: instantiated from here comp.cpp:23: error: no type named `result_type' in `class CPrint' /usr/local/include/c++/3.3.2/ext/functional:135: error: no type named ` result_type' in `class CPrint' /usr/local/include/c++/3.3.2/bits/stl_algo.h: In function `_Function std::for_each(_InputIter, _InputIter, _Function) [with _InputIter = std::_Rb_tree_iterator<std::pair<const int, int>, std::pair<const int, int>&, std::pair<const int, int>*>, _Function = __gnu_cxx::unary_compose<CPrint, __gnu_cxx::select2nd<std::pair<const int, int> > >]': comp.cpp:23: instantiated from here /usr/local/include/c++/3.3.2/bits/stl_algo.h:157: error: no match for call to `(__gnu_cxx::unary_compose<CPrint, __gnu_cxx::select2nd<std::pair<const int, int> > >) (std::pair<const int, int>&)' why?
Advertisement
You need to provide a couple of typedefs in your functor class for the adaptors to work. The easiest way of doing this is deriving the class from std::unary_function<void, int> (declared in <functional>; the first template argument is the result type and the second the argument type of your operation). Please check out SGI's docs for more info.
The result type is actually, and counter-intuitively, the last template parameter.

std::unary_function<ArgType, ResultType>
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Quote:Original post by Sharlin
You need to provide a couple of typedefs in your functor class for the adaptors to work. The easiest way of doing this is deriving the class from std::unary_function<void, int> (declared in <functional>; the first template argument is the result type and the second the argument type of your operation). Please check out SGI's docs for more info.


Thank you. but when I derive class unary_function as following
class CPrint : public unary_function<int, void>

I got another compiling error:

...
comp.cpp:23: instantiated from here
/usr/local/include/c++/3.2.2/ext/functional:136: passing `const CPrint' as
`this' argument of `void CPrint::operator()(int)' discards qualifiers

how to resolve it?


Quote:Original post by sprime
Quote:Original post by Sharlin
You need to provide a couple of typedefs in your functor class for the adaptors to work. The easiest way of doing this is deriving the class from std::unary_function<void, int> (declared in <functional>; the first template argument is the result type and the second the argument type of your operation). Please check out SGI's docs for more info.


Thank you. but when I derive class unary_function as following
class CPrint : public unary_function<int, void>

I got another compiling error:

...
comp.cpp:23: instantiated from here
/usr/local/include/c++/3.2.2/ext/functional:136: passing `const CPrint' as
`this' argument of `void CPrint::operator()(int)' discards qualifiers

how to resolve it?




OK, I find the solution:)
the operation should be declared as const, that's all right!

This topic is closed to new replies.

Advertisement