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.. 4 4817
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);
}
};
On 3ÔÂ4ÈÕ, ÏÂÎç8ʱ53·Ö, John Harrison <john_androni....@hotmail.comwrote:
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.. wrote:
On 3ÔÂ4ÈÕ, ÏÂÎç8ʱ53·Ö, John Harrison <john_androni...@hotmail.comwrote:
>>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
On 3ÔÂ4ÈÕ, ÏÂÎç9ʱ24·Ö, John Harrison <john_androni....@hotmail.comwrote:
Lycan. Mao.. wrote:
On 3ÔÂ4ÈÕ, ÏÂÎç8ʱ53·Ö, John Harrison <john_androni...@hotmail.comwrote:
>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! This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Laxman |
last post by:
==C.H <==
#include <fstream.h>
class C
{
public:
C(int i);
friend ofstream& operator<<(ofstream& os, const C&);
};
------------------------------------------------------------
==B.H <==
|
by: Nick Savoiu |
last post by:
In the code below how can I get x to use the const T& operator?
Thanks,
Nick
#include <stdio.h>
template <typename T> class C
{
public:
|
by: Alex Vinokur |
last post by:
Why is it ambiguous?
------ foo.cpp ------
struct Foo
{
Foo operator* (Foo) { return Foo(); }
Foo operator* (int) const { return Foo(); }
Foo () {}
Foo (int) {}
};
|
by: Mark Stijnman |
last post by:
I would like to be able to have an object accessible as a vector using
the operator, but able to track modifications to its data, so that
it can update other internal data as needed. I figured...
|
by: Tim Clacy |
last post by:
Please illuminate; what operator of class 'Event' will get matched for these
two cases :
Event ev1;
Event ev2;
// Case 1
//
if (ev1)
;
|
by: Claudius |
last post by:
Hello,
I have written a class A with the access operator(int) overloaded by
a
A-const version which returns an int by value:
------------------------------------------------
#include...
|
by: Pedro |
last post by:
Hello pythonians! ;-D ,
I have a little problem when I expose (assisted by boost.python) classes
with virtual functions, specially with operator().
In the C++ code below I test two different...
|
by: jakester |
last post by:
I am using Visual C++ 2007 to build the code below. I keep getting
linkage error. Could someone please tell me what I am doing wrong? The
code works until I start using namespace for my objects.
...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
| |