473,609 Members | 2,766 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

error: passing `const Equal4' as `this' argument of `bool Equal4::operato r()(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::arg ument_type argument_type;
typedef typename _Predicate::ret urn_type return_type;

_Predicate pred_;

explicit
Unary_negate(co nst _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<Eq ual4>(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_pred icate::return_t ype
STLite::Unary_n egate<_Adaptabl e_predicate>::o perator()(int) const
[with _Adaptable_pred icate = Equal4]':
algorithm.hpp:2 0: instantiated from `_Input_iterato r
STLite::find_if (_Input_iterato r, _Input_iterator , _Predicate) [with
_Input_iterator = Node_list_itera tor<int, Node<int>, int*, int&>,
_Predicate = STLite::Unary_n egate<Equal4>]'
main.cpp:100: instantiated from here
functor.hpp:50: error: passing `const Equal4' as `this' argument of
`bool Equal4::operato r()(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(con st 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..

Mar 4 '07 #1
4 4852
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::arg ument_type argument_type;
typedef typename _Predicate::ret urn_type return_type;

_Predicate pred_;

explicit
Unary_negate(co nst _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);
}
};
Mar 4 '07 #2
On 3ÔÂ4ÈÕ, ÏÂÎç8ʱ53·Ö, John Harrison <john_androni.. ..@hotmail.comw rote:
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::arg ument_type argument_type;
typedef typename _Predicate::ret urn_type return_type;
_Predicate pred_;
explicit
Unary_negate(co nst _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!

Mar 4 '07 #3
Lycan. Mao.. wrote:
On 3ÔÂ4ÈÕ, ÏÂÎç8ʱ53·Ö, John Harrison <john_androni.. .@hotmail.comwr ote:
>>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::arg ument_type argument_type;
typedef typename _Predicate::ret urn_type return_type;
>> _Predicate pred_;
>> explicit
Unary_negate(co nst _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_pred icate::return_t ype
STLite::Unary_n egate<_Adaptabl e_predicate>::o perator()(int) const
[with _Adaptable_pred icate = Equal4]':

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

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
Mar 4 '07 #4
On 3ÔÂ4ÈÕ, ÏÂÎç9ʱ24·Ö, John Harrison <john_androni.. ..@hotmail.comw rote:
Lycan. Mao.. wrote:
On 3ÔÂ4ÈÕ, ÏÂÎç8ʱ53·Ö, John Harrison <john_androni.. .@hotmail.comwr ote:
>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::arg ument_type argument_type;
typedef typename _Predicate::ret urn_type return_type;
> _Predicate pred_;
> explicit
Unary_negate(co nst _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_pred icate::return_t ype
STLite::Unary_n egate<_Adaptabl e_predicate>::o perator()(int) const
[with _Adaptable_pred icate = Equal4]':

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

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!

Mar 4 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
4092
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 <==
8
6826
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:
4
2369
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) {} };
2
2583
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 that setting a flag in the non-const operator would work, but it doesn't. Take the code below: #include <iostream> using std::cout; using std::cerr;
15
4367
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) ;
2
2884
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 <iostream> using namespace std;
0
1762
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 implementations of a member function of A that takes as an argument the abstract class Base (see Option 1 / Option 2): - Both compile without problems - Only the second works in python (see python program below).
11
3803
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. Error 1 error LNK2019: unresolved external symbol "class std::basic_ostream<char,struct std::char_traits<char & __cdecl graph::operator<<(class std::basic_ostream<char,struct std::char_traits<char &,class graph::Node &)" (??6graph@@YAAAV?...
0
8047
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8510
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8198
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8376
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6975
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6044
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5503
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
1636
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1372
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.