Hill
10/25/2008 1:28:00 AM
On 10?24?, ??4?33?, Bradley Smith <s...@baysmith.com> wrote:
> Hill wrote:
> > This is just a exercise. I defined my own mem_fun1_ref_t in my_fun
> > space.
> > But got compiling error. How to fix it ? Any help is appreciated.
>
> > Administrator@HILL /m/tcplex/p3_ch18
> > $ cat mem_fun1_ref.cpp
> > #include <list>
> > #include <functional>
> > #include <iostream>
> > #include <algorithm>
>
> > namespace my_fun{
> > //class mem_fun1_ref_t
> > template<typename R, typename T, typename Targ> class mem_fun1_ref_t:
> > public std::binary_function<T, Targ, R>
> > {
> > R (T::*pmf)(Targ);
> > public:
> > explicit mem_fun1_ref_t(R (T::*p)(Targ)):pmf(p){}
> > R operator()(T& p, Targ arg){return (p.*pmf)(arg);}
>
> Change this to:
> R operator()(T& p, Targ arg)const{return (p.*pmf)(arg);}
> ----------------------------------^^^^^
>
>
>
> > };
> > template<typename R, typename T, typename Targ>
> > mem_fun1_ref_t<R, T, Targ> mem_fun_ref(R (T::*f)(Targ))
> > {
> > return mem_fun1_ref_t<R, T, Targ>(f);
> > }
> > }//end namespace my_fun
> > class base
> > {
> > public:
> > void print_with(const std::string str){
> > std::cout << "Just for test: " << this << str << std::endl;
> > }
> > };
> > int main()
> > {
> > base bn;
> > std::string str("TEST_STRING");
> > std::bind2nd(my_fun::mem_fun_ref(&base::print_with),str)(bn);
> > }
> > Administrator@HILL /m/tcplex/p3_ch18
> > $ g++ mem_fun1_ref.cpp
> > e:/opt/mingw/bin/../lib/gcc/mingw32/3.4.5/../../../../include/c++/
> > 3.4.5/bits/stl_function.h: In member function `typename
> > _Operation::result_type std::binder2nd<_Operation>::operator()
> > (typename _Operation::first_argument_type&) const [with _Operation =
> > my_fun::mem_fun1_ref_t<void, base, std::string>]':
> > mem_fun1_ref.cpp:33: instantiated from here
> > e:/opt/mingw/bin/../lib/gcc/mingw32/3.4.5/../../../../include/c++/
> > 3.4.5/bits/stl_function.h:446: error: passing `const
> > my_fun::mem_fun1_ref_t<void, base, std::string>' as `this' argument of
> > `R my_fun::mem_fun1_ref_t<R, T, Targ>::operator()(T&, Targ) [with R =
> > void, T = base, Targ = std::string]' discards qualifiers- ??????? -
>
> - ??????? -- ??????? -
>
> - ??????? -
Thanks very much :)