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

L

Lycan. Mao..

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..
 
J

John Harrison

Lycan. Mao.. said:
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);
}
};
 
L

Lycan. Mao..

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!
 
J

John Harrison

Lycan. Mao.. said:
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
 
L

Lycan. Mao..

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!
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top