error: passing `const Equal4' as `this' argument of `bool Equal4::operator()(int)' discards qualifie

Discussion in 'C++' started by Lycan. Mao.., Mar 4, 2007.

  1. Lycan. Mao..

    Lycan. Mao.. Guest

    Hello, I'm trying to write a function adapter object, but it fails
    with the above information. Can you help me.

    template <typename _Predicate>
    struct Unary_negate {
    typedef typename _Predicate::argument_type argument_type;
    typedef typename _Predicate::return_type return_type;

    _Predicate pred_;

    explicit
    Unary_negate(const _Predicate& p) : pred_(p) {};

    bool operator() (const argument_type& x) const
    {return !pred_(x);}
    };

    and then:

    struct Equal4
    : Unary_function<int, bool>
    {
    bool operator() (const int i)
    {
    return (i == 4);
    }
    };

    then I use them like this:

    STLite::find_if(p.begin(), p.end(), Unary_negate<Equal4>(Equal4()));

    You see, I rewrite the find_if and iterators and Unary_function, but
    they are almost the same like the STL ones.

    then, as a result of compiling, I get:

    functor.hpp: In member function `typename
    _Adaptable_predicate::return_type
    STLite::Unary_negate<_Adaptable_predicate>::eek:perator()(int) const
    [with _Adaptable_predicate = Equal4]':
    algorithm.hpp:20: instantiated from `_Input_iterator
    STLite::find_if(_Input_iterator, _Input_iterator, _Predicate) [with
    _Input_iterator = Node_list_iterator<int, Node<int>, int*, int&>,
    _Predicate = STLite::Unary_negate<Equal4>]'
    main.cpp:100: instantiated from here
    functor.hpp:50: error: passing `const Equal4' as `this' argument of
    `bool Equal4::eek:perator()(int)' discards qualifiers

    At first, I think it's my fault when copying the STL, but I test like
    this:

    struct Equal4_wrap {
    typedef int arg;
    typedef bool ret;
    Equal4 pred_;
    explicit Equal4_wrap(const Equal4& p) : pred_(p) {}
    ret operator() (const int& i) {return !pred_(i);}
    };

    which I think exactly the same as the instantialized one of
    Unary_negate, and it works well, really.

    I'm confused, I hope some one can help. Thank you very much!

    Mao..
     
    Lycan. Mao.., Mar 4, 2007
    #1
    1. Advertising

  2. Re: error: passing `const Equal4' as `this' argument of `bool Equal4::eek:perator()(int)'discards qualifiers

    Lycan. Mao.. wrote:
    > Hello, I'm trying to write a function adapter object, but it fails
    > with the above information. Can you help me.
    >
    > template <typename _Predicate>
    > struct Unary_negate {
    > typedef typename _Predicate::argument_type argument_type;
    > typedef typename _Predicate::return_type return_type;
    >
    > _Predicate pred_;
    >
    > explicit
    > Unary_negate(const _Predicate& p) : pred_(p) {};
    >
    > bool operator() (const argument_type& x) const
    > {return !pred_(x);}
    > };
    >
    > and then:
    >
    > struct Equal4
    > : Unary_function<int, bool>
    > {
    > bool operator() (const int i)
    > {
    > return (i == 4);
    > }
    > };
    >


    const in the wrong place, you don't need 'const int' but you do need to
    make operator() a const method.

    struct Equal4 : Unary_function<int, bool>
    {
    bool operator() (int i) const
    {
    return (i == 4);
    }
    };
     
    John Harrison, Mar 4, 2007
    #2
    1. Advertising

  3. Lycan. Mao..

    Lycan. Mao.. Guest

    On 3ÔÂ4ÈÕ, ÏÂÎç8ʱ53·Ö, John Harrison <> wrote:
    > Lycan. Mao.. wrote:
    > > Hello, I'm trying to write a function adapter object, but it fails
    > > with the above information. Can you help me.

    >
    > > template <typename _Predicate>
    > > struct Unary_negate {
    > > typedef typename _Predicate::argument_type argument_type;
    > > typedef typename _Predicate::return_type return_type;

    >
    > > _Predicate pred_;

    >
    > > explicit
    > > Unary_negate(const _Predicate& p) : pred_(p) {};

    >
    > > bool operator() (const argument_type& x) const
    > > {return !pred_(x);}
    > > };

    >
    > > and then:

    >
    > > struct Equal4
    > > : Unary_function<int, bool>
    > > {
    > > bool operator() (const int i)
    > > {
    > > return (i == 4);
    > > }
    > > };

    >
    > const in the wrong place, you don't need 'const int' but you do need to
    > make operator() a const method.
    >
    > struct Equal4 : Unary_function<int, bool>
    > {
    > bool operator() (int i) const
    > {
    > return (i == 4);
    > }
    >
    > };


    Yes, thank you very much! I have tested it, and yes, it's right! Thank
    you again.

    But can you show me why? Why one is all right and the other fails?(I
    mean both Equal4_wrap/Unary_negate and operator() (const int)/
    operator() (int) const)

    Thank you!
     
    Lycan. Mao.., Mar 4, 2007
    #3
  4. Re: error: passing `const Equal4' as `this' argument of `bool Equal4::eek:perator()(int)'discards qualifiers

    Lycan. Mao.. wrote:
    > On 3ÔÂ4ÈÕ, ÏÂÎç8ʱ53·Ö, John Harrison <> wrote:
    >
    >>Lycan. Mao.. wrote:
    >>
    >>>Hello, I'm trying to write a function adapter object, but it fails
    >>>with the above information. Can you help me.

    >>
    >>>template <typename _Predicate>
    >>>struct Unary_negate {
    >>> typedef typename _Predicate::argument_type argument_type;
    >>> typedef typename _Predicate::return_type return_type;

    >>
    >>> _Predicate pred_;

    >>
    >>> explicit
    >>> Unary_negate(const _Predicate& p) : pred_(p) {};

    >>
    >>> bool operator() (const argument_type& x) const
    >>> {return !pred_(x);}
    >>>};

    >>
    >>>and then:

    >>
    >>>struct Equal4
    >>> : Unary_function<int, bool>
    >>>{
    >>> bool operator() (const int i)
    >>> {
    >>> return (i == 4);
    >>> }
    >>>};

    >>
    >>const in the wrong place, you don't need 'const int' but you do need to
    >>make operator() a const method.
    >>
    >>struct Equal4 : Unary_function<int, bool>
    >>{
    >> bool operator() (int i) const
    >> {
    >> return (i == 4);
    >> }
    >>
    >>};

    >
    >
    > Yes, thank you very much! I have tested it, and yes, it's right! Thank
    > you again.
    >
    > But can you show me why? Why one is all right and the other fails?(I
    > mean both Equal4_wrap/Unary_negate and operator() (const int)/
    > operator() (int) const)
    >
    > Thank you!
    >


    Look at the error message

    functor.hpp: In member function `typename
    _Adaptable_predicate::return_type
    STLite::Unary_negate<_Adaptable_predicate>::eek:perator()(int) const
    [with _Adaptable_predicate = Equal4]':

    STLite::Unary_negate::eek:perator() is a const member function, so when it
    tries to call Equal4::eek:perator() is expects that to also be a const
    membe function.

    Now look at your Equal4_wrap, it's operator() is not a const member
    function, so it has no problem calling Equal4::eek:perator().

    If you had written

    struct Equal4_wrap {
    ...
    ret operator() const (const int& i) {return !pred_(i);}
    };

    you would have got the same error in your test.

    john
     
    John Harrison, Mar 4, 2007
    #4
  5. Lycan. Mao..

    Lycan. Mao.. Guest

    On 3ÔÂ4ÈÕ, ÏÂÎç9ʱ24·Ö, John Harrison <> wrote:
    > Lycan. Mao.. wrote:
    > > On 3ÔÂ4ÈÕ, ÏÂÎç8ʱ53·Ö, John Harrison <> wrote:

    >
    > >>Lycan. Mao.. wrote:

    >
    > >>>Hello, I'm trying to write a function adapter object, but it fails
    > >>>with the above information. Can you help me.

    >
    > >>>template <typename _Predicate>
    > >>>struct Unary_negate {
    > >>> typedef typename _Predicate::argument_type argument_type;
    > >>> typedef typename _Predicate::return_type return_type;

    >
    > >>> _Predicate pred_;

    >
    > >>> explicit
    > >>> Unary_negate(const _Predicate& p) : pred_(p) {};

    >
    > >>> bool operator() (const argument_type& x) const
    > >>> {return !pred_(x);}
    > >>>};

    >
    > >>>and then:

    >
    > >>>struct Equal4
    > >>> : Unary_function<int, bool>
    > >>>{
    > >>> bool operator() (const int i)
    > >>> {
    > >>> return (i == 4);
    > >>> }
    > >>>};

    >
    > >>const in the wrong place, you don't need 'const int' but you do need to
    > >>make operator() a const method.

    >
    > >>struct Equal4 : Unary_function<int, bool>
    > >>{
    > >> bool operator() (int i) const
    > >> {
    > >> return (i == 4);
    > >> }

    >
    > >>};

    >
    > > Yes, thank you very much! I have tested it, and yes, it's right! Thank
    > > you again.

    >
    > > But can you show me why? Why one is all right and the other fails?(I
    > > mean both Equal4_wrap/Unary_negate and operator() (const int)/
    > > operator() (int) const)

    >
    > > Thank you!

    >
    > Look at the error message
    >
    > functor.hpp: In member function `typename
    > _Adaptable_predicate::return_type
    > STLite::Unary_negate<_Adaptable_predicate>::eek:perator()(int) const
    > [with _Adaptable_predicate = Equal4]':
    >
    > STLite::Unary_negate::eek:perator() is a const member function, so when it
    > tries to call Equal4::eek:perator() is expects that to also be a const
    > membe function.
    >
    > Now look at your Equal4_wrap, it's operator() is not a const member
    > function, so it has no problem calling Equal4::eek:perator().
    >
    > If you had written
    >
    > struct Equal4_wrap {
    > ...
    > ret operator() const (const int& i) {return !pred_(i);}
    >
    > };
    >
    > you would have got the same error in your test.
    >
    > john


    Thank you very much! I understand it now.

    Thank you!
     
    Lycan. Mao.., Mar 4, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. danny van elsen
    Replies:
    6
    Views:
    41,242
    Pete Becker
    Apr 24, 2005
  2. Tim Clacy
    Replies:
    15
    Views:
    2,692
    Kanenas
    May 30, 2005
  3. Neo
    Replies:
    1
    Views:
    556
    Victor Bazarov
    Mar 28, 2006
  4. Replies:
    11
    Views:
    1,113
  5. Javier
    Replies:
    2
    Views:
    574
    James Kanze
    Sep 4, 2007
Loading...

Share This Page