Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > error: passing `const Equal4' as `this' argument of `bool Equal4::operator()(int)' discards qualifiers

Reply
Thread Tools

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

 
 
Lycan. Mao..
Guest
Posts: n/a
 
      03-04-2007
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>:perat 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: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..

 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      03-04-2007
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);
}
};
 
Reply With Quote
 
 
 
 
Lycan. Mao..
Guest
Posts: n/a
 
      03-04-2007
On 3月4日, 下午8时53分, John Harrison <(E-Mail Removed)> 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!

 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      03-04-2007
Lycan. Mao.. wrote:
> On 3月4日, 下午8时53分, John Harrison <(E-Mail Removed)> 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>:perat or()(int) const
[with _Adaptable_predicate = Equal4]':

STLite::Unary_negate:perator() is a const member function, so when it
tries to call Equal4: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: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
 
Reply With Quote
 
Lycan. Mao..
Guest
Posts: n/a
 
      03-04-2007
On 3月4日, 下午9时24分, John Harrison <(E-Mail Removed)> wrote:
> Lycan. Mao.. wrote:
> > On 3月4日, 下午8时53分, John Harrison <(E-Mail Removed)> 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>:perat or()(int) const
> [with _Adaptable_predicate = Equal4]':
>
> STLite::Unary_negate:perator() is a const member function, so when it
> tries to call Equal4: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: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!

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
discards qualifiers Ian Collins C++ 2 05-07-2006 03:27 AM
Error: passing `const state' as `this' argument of `void state::dumpState()' discards qualifiers Neo C++ 1 03-28-2006 04:34 AM
error: passing const ... discards qualifiers danny van elsen C++ 6 04-24-2005 10:34 PM
Vexing GCC warnings: "assignment discards qualifiers from pointertarget type" Charlie Zender C Programming 12 01-03-2004 12:18 AM
warning: assignment discards qualifiers from pointer target type Jason C Programming 6 10-09-2003 07:38 PM



Advertisments