Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   error: passing `const Equal4' as `this' argument of `bool Equal4::operator()(int)' discards qualifiers (http://www.velocityreviews.com/forums/t460893-error-passing-const-equal4-as-this-argument-of-bool-equal4-operator-int-discards-qualifiers.html)

Lycan. Mao.. 03-04-2007 12:40 PM

error: passing `const Equal4' as `this' argument of `bool Equal4::operator()(int)' discards qualifiers
 
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>::operat or()(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::operator()(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..


John Harrison 03-04-2007 12:53 PM

Re: error: passing `const Equal4' as `this' argument of `bool Equal4::operator()(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);
}
};

Lycan. Mao.. 03-04-2007 01:05 PM

Re: error: passing `const Equal4' as `this' argument of `bool Equal4::operator()(int)' discards qualifiers
 
On 3月4日, 下午8时53分, John Harrison <john_androni....@hotmail.com> 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!


John Harrison 03-04-2007 01:24 PM

Re: error: passing `const Equal4' as `this' argument of `bool Equal4::operator()(int)'discards qualifiers
 
Lycan. Mao.. wrote:
> On 3月4日, 下午8时53分, John Harrison <john_androni...@hotmail.com> 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>::operat or()(int) const
[with _Adaptable_predicate = Equal4]':

STLite::Unary_negate::operator() is a const member function, so when it
tries to call Equal4::operator() 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::operator().

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

Lycan. Mao.. 03-04-2007 02:37 PM

Re: error: passing `const Equal4' as `this' argument of `bool Equal4::operator()(int)' discards qualifiers
 
On 3月4日, 下午9时24分, John Harrison <john_androni....@hotmail.com> wrote:
> Lycan. Mao.. wrote:
> > On 3月4日, 下午8时53分, John Harrison <john_androni...@hotmail.com> 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>::operat or()(int) const
> [with _Adaptable_predicate = Equal4]':
>
> STLite::Unary_negate::operator() is a const member function, so when it
> tries to call Equal4::operator() 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::operator().
>
> 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!



All times are GMT. The time now is 03:53 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.