About my own mem_fun1_ref_t.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Hill

    About my own mem_fun1_ref_t.

    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@H ILL /m/tcplex/p3_ch18
    $ cat mem_fun1_ref.cp p
    #include <list>
    #include <functional>
    #include <iostream>
    #include <algorithm>

    namespace my_fun{
    //class mem_fun1_ref_t
    template<typena me R, typename T, typename Targclass mem_fun1_ref_t:
    public std::binary_fun ction<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);}
    };
    template<typena me R, typename T, typename Targ>
    mem_fun1_ref_t< R, T, Targmem_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(cons t std::string str){
    std::cout << "Just for test: " << this << str << std::endl;
    }
    };
    int main()
    {
    base bn;
    std::string str("TEST_STRIN G");
    std::bind2nd(my _fun::mem_fun_r ef(&base::print _with),str)(bn) ;
    }
    Administrator@H ILL /m/tcplex/p3_ch18
    $ g++ mem_fun1_ref.cp p
    e:/opt/mingw/bin/../lib/gcc/mingw32/3.4.5/../../../../include/c++/
    3.4.5/bits/stl_function.h: In member function `typename
    _Operation::res ult_type std::binder2nd< _Operation>::op erator()
    (typename _Operation::fir st_argument_typ e&) const [with _Operation =
    my_fun::mem_fun 1_ref_t<void, base, std::string>]':
    mem_fun1_ref.cp p: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_fun 1_ref_t<void, base, std::string>' as `this' argument of
    `R my_fun::mem_fun 1_ref_t<R, T, Targ>::operator ()(T&, Targ) [with R =
    void, T = base, Targ = std::string]' discards qualifiers
  • Bradley Smith

    #2
    Re: About my own mem_fun1_ref_t.

    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@H ILL /m/tcplex/p3_ch18
    $ cat mem_fun1_ref.cp p
    #include <list>
    #include <functional>
    #include <iostream>
    #include <algorithm>
    >
    namespace my_fun{
    //class mem_fun1_ref_t
    template<typena me R, typename T, typename Targclass mem_fun1_ref_t:
    public std::binary_fun ction<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{retur n (p.*pmf)(arg);}
    ----------------------------------^^^^^
    };
    template<typena me R, typename T, typename Targ>
    mem_fun1_ref_t< R, T, Targmem_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(cons t std::string str){
    std::cout << "Just for test: " << this << str << std::endl;
    }
    };
    int main()
    {
    base bn;
    std::string str("TEST_STRIN G");
    std::bind2nd(my _fun::mem_fun_r ef(&base::print _with),str)(bn) ;
    }
    Administrator@H ILL /m/tcplex/p3_ch18
    $ g++ mem_fun1_ref.cp p
    e:/opt/mingw/bin/../lib/gcc/mingw32/3.4.5/../../../../include/c++/
    3.4.5/bits/stl_function.h: In member function `typename
    _Operation::res ult_type std::binder2nd< _Operation>::op erator()
    (typename _Operation::fir st_argument_typ e&) const [with _Operation =
    my_fun::mem_fun 1_ref_t<void, base, std::string>]':
    mem_fun1_ref.cp p: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_fun 1_ref_t<void, base, std::string>' as `this' argument of
    `R my_fun::mem_fun 1_ref_t<R, T, Targ>::operator ()(T&, Targ) [with R =
    void, T = base, Targ = std::string]' discards qualifiers

    Comment

    • Hill

      #3
      Re: About my own mem_fun1_ref_t.

      On 10ÔÂ24ÈÕ, ÏÂÎç4ʱ33·Ö, Bradley Smith <s...@baysmith. comwrote:
      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@H ILL /m/tcplex/p3_ch18
      $ cat mem_fun1_ref.cp p
      #include <list>
      #include <functional>
      #include <iostream>
      #include <algorithm>
      >
      namespace my_fun{
      //class mem_fun1_ref_t
      template<typena me R, typename T, typename Targclass mem_fun1_ref_t:
      public std::binary_fun ction<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{retur n (p.*pmf)(arg);}
      ----------------------------------^^^^^
      >
      >
      >
      };
      template<typena me R, typename T, typename Targ>
      mem_fun1_ref_t< R, T, Targmem_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(cons t std::string str){
      std::cout << "Just for test: " << this << str << std::endl;
      }
      };
      int main()
      {
      base bn;
      std::string str("TEST_STRIN G");
      std::bind2nd(my _fun::mem_fun_r ef(&base::print _with),str)(bn) ;
      }
      Administrator@H ILL /m/tcplex/p3_ch18
      $ g++ mem_fun1_ref.cp p
      e:/opt/mingw/bin/../lib/gcc/mingw32/3.4.5/../../../../include/c++/
      3.4.5/bits/stl_function.h: In member function `typename
      _Operation::res ult_type std::binder2nd< _Operation>::op erator()
      (typename _Operation::fir st_argument_typ e&) const [with _Operation =
      my_fun::mem_fun 1_ref_t<void, base, std::string>]':
      mem_fun1_ref.cp p: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_fun 1_ref_t<void, base, std::string>' as `this' argument of
      `R my_fun::mem_fun 1_ref_t<R, T, Targ>::operator ()(T&, Targ) [with R =
      void, T = base, Targ = std::string]' discards qualifiers- Òþ²Ø±»ÒýÓÃÎÄ×Ö -
      >
      - ÏÔʾÒýÓõÄÎÄ×Ö -- Òþ²Ø±»ÒýÓÃÎÄ×Ö -
      >
      - ÏÔʾÒýÓõÄÎÄ×Ö -
      Thanks very much :)

      Comment

      Working...