Jump to content
  • Advertisement
Sign in to follow this  
sprime

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

This topic is 4845 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
The result type is actually, and counter-intuitively, the last template parameter.

std::unary_function<ArgType, ResultType>

Share this post


Link to post
Share on other sites
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?


Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!