473,897 Members | 2,796 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

error: passing `const ...' as `this' argument of `...' discardsqualifi ers

Hello,

I'm doing some toy experiments to see how
the algoritm std::transform and the function
adapter std::bind2nd can play together, but
my compiler give my the error

error: passing `const traslate' as `this' argument of
`circle traslate::opera tor()(circle, std::vector<dou ble,
std::allocator< double)'
discards qualifiers

It should be like I'm trying to modify
something which is declared as const...

The example code follows.

I define the type `circle', which holds
center and radius of a circle, then I
introduce a vector of circles and try to
traslate them all with std::transform.
I need std::bind2nd to fix the traslation
for all the circles.
Note that I'm forced to define the
temporary object `traslate' to meet
std::bind2nd input specifications.

// =============== =============== =========
#include <vector>
#include<functi onal>
#include<algori thm>
using namespace std;

struct circle
{
vector<doublece nter;
double radius;
};

struct traslate
{
typedef circle first_argument_ type;
typedef vector<doublese cond_argument_t ype;
typedef circle result_type;
circle operator()(cons t circle original, const vector<double>
traslation)
{
circle traslated = original;
traslated.cente r[0] += traslation[0];
traslated.cente r[1] += traslation[1];
return traslated;
}
};

int main()
{
vector<circlelo ts_of_circles(1 0);
vector<circleso me_other_circle s(10);
vector<doubletr aslation(2);
traslation[0] = 123;
traslation[1] = 321;
transform(lots_ of_circles.begi n(), lots_of_circles .end(),
some_other_circ les.begin(), bind2nd(traslat e(), traslation));
}
// =============== =============== =========

Can you see the reason of the above mentioned error?

Regards,
Giovanni Gh.
Aug 12 '08 #1
4 6205
On Aug 12, 9:23*am, Giovanni Gherdovich
<gherdov...@stu dents.math.unif i.itwrote:
Hello,

I'm doing some toy experiments to see how
the algoritm std::transform and the function
adapter std::bind2nd can play together, but
my compiler give my the error

error: passing `const traslate' as `this' argument of
`circle traslate::opera tor()(circle, std::vector<dou ble,
std::allocator< double)'
discards qualifiers

It should be like I'm trying to modify
something which is declared as const...

The example code follows.

I define the type `circle', which holds
center and radius of a circle, then I
introduce a vector of circles and try to
traslate them all with std::transform.
I need std::bind2nd to fix the traslation
for all the circles.
Note that I'm forced to define the
temporary object `traslate' to meet
std::bind2nd input specifications.

// =============== =============== =========
#include <vector>
#include<functi onal>
#include<algori thm>
using namespace std;

struct circle
{
* vector<doublece nter;
How about std::pair instead? Or, perhaps even better, make your own
point class with members x and y. std::vector is overkill here.
* double radius;

};

struct traslate
{
*typedef circle first_argument_ type;
*typedef vector<doublese cond_argument_t ype;
*typedef circle result_type;
Consider inheriting from std::binary_fun ction instead of this.
*circle operator()(cons t circle original, const vector<double>
traslation)
You probably want to use references for both of these parameters to
prevent unnecessary copies. (They may be inlined away or relatively
trivial here, but maybe not if you call this a lot.) You should also
use std::pair or your own point class here, too, since you only have x
and y coordinates. Since this function doesn't modify the object's
state, it should be made const (note also the references):

circle operator()(
const circle& original,
const vector<double>& traslation) const
{ ... }

However, since your functor has no state, you should just make this a
plain old function rather than a function object.
*{
* *circle traslated = original;
* *traslated.cent er[0] += traslation[0];
* *traslated.cent er[1] += traslation[1];
* *return traslated;
*}

};

int main()
{
* vector<circlelo ts_of_circles(1 0);
* vector<circleso me_other_circle s(10);
* vector<doubletr aslation(2);
* traslation[0] = 123;
* traslation[1] = 321;
* transform(lots_ of_circles.begi n(), lots_of_circles .end(),
some_other_circ les.begin(), bind2nd(traslat e(), traslation));}

// =============== =============== =========

Can you see the reason of the above mentioned error?
You're creating a temporary object -- the instance of translate --
which is bound to a const reference in bind2nd. The function you are
calling is not const. Add const to the member function, or better,
make it a plain old function.

See these FAQs for more info on const-correctness:

http://www.parashift.com/c++-faq-lit...rrectness.html

Cheers! --M
Aug 12 '08 #2
Thank you; you answer even more than
what I've asked and I appreciate it a lot.
struct circle
{
vector<doublece nter;

How about std::pair instead
I agree. And since I don't need any method
for these points, std::pair will do the job.
struct traslate
{
typedef circle first_argument_ type;
typedef vector<doublese cond_argument_t ype;
typedef circle result_type;

Consider inheriting from std::binary_fun ction
instead of this.
Cool!
So I can avoid to explicitly rename my types with the
tedious

typedef circle first_argument_ type;
typedef vector<doublese cond_argument_t ype;
typedef circle result_type;
However, since your functor has no state, you should just make this a
plain old function rather than a function object.
True, but if I do that I cannot use the algorithm std::tranform,
wich save me from looping explicitly over the circles (wich I
find is cool), because I cannot use std::bind2nd
(which wants a function object as first argument).
You're creating a temporary object -- the instance of translate --
which is bound to a const reference in bind2nd. The function you are
calling is not const. Add const to the member function
Exactly.
Thank you, also for the reference to the FAQs.

But let me understand: std::bind2nd onbly accept _const_ member
functions a input function object?

Cheers,
Giovanni
Aug 12 '08 #3
On Aug 12, 10:57*am, Giovanni Gherdovich
<gherdov...@stu dents.math.unif i.itwrote:
How about std::pair instead

I agree. And since I don't need any method
for these points, std::pair will do the job.
Right, though struct Point { double x,y; }; may make your code easier
to read and only requires reinventing a very, very small wheel. :-)
However, since your functor has no state, you should just make this a
plain old function rather than a function object.

True, but if I do that I cannot use the algorithm std::tranform,
wich save me from looping explicitly over the circles (wich I
find is cool), because I cannot use std::bind2nd
(which wants a function object as first argument).
With ordinary functions, just use std::ptr_fun to wrap them for
std::transform et al.:

circle Translate1( const circle original, const vector<double>
traslation)
{ /* code from traslate::opera tor() */ }

// ...
transform(lots_ of_circles.begi n(), lots_of_circles .end(),
some_other_circ les.begin(),
bind2nd(ptr_fun (Translate), traslation));

You may notice I omitted the references from the parameters to
Translate1 because, unfortunately, the standard binders currently
don't allow reference arguments. For more on this problem, see
http://www.boost.org/doc/libs/1_35_0...raits.htm#refs

Alternately, if you have the standard library extensions known as TR1
or if you have Boost (where most of those extensions originated), you
could use reference arguments with the generalized binder
(std::tr1::bind or boost::bind) instead of std::bind2nd:

// Note ref params
circle Translate2( const circle& original, const vector<double>&
traslation)
{/*same code*/}

// ...

// TR1 version
transform(lots_ of_circles.begi n(), lots_of_circles .end(),
some_other_circ les.begin(),
std::tr1::bind(
Translate2,
std::tr1::place holders::_1,
traslation) );

// Easier-to-read version of the above:
using namespace std::tr1;
using namespace std::tr1::place holders;

transform(lots_ of_circles.begi n(), lots_of_circles .end(),
some_other_circ les.begin(), bind( Translate2, _1, traslation) );
// Boost version
transform(lots_ of_circles.begi n(), lots_of_circles .end(),
some_other_circ les.begin(), boost::bind(Tra nslate2, _1,
traslation) );

For more on these, see Pete Becker's article:

http://www.ddj.com/cpp/184401949

or his book on TR1, or the Boost.Bind documentation.
But let me understand: std::bind2nd onbly accept _const_ member
functions a input function object?
Yes. The enhanced binders correct this and other "inconveniences " with
the standard binders.

Cheers! --M
Aug 12 '08 #4
Dear Mlimber,
However, since your functor has no state, you should just make this a
plain old function rather than a function object.
True, but if I do that I cannot use the algorithm std::tranform,
wich save me from looping explicitly over the circles (wich I
find is cool), because I cannot use std::bind2nd
(which wants a function object as first argument).

With ordinary functions, just use std::ptr_fun to wrap them for
std::transform et al.:
this information is precious.
I was wondering how to use std::bind2nd without building
ad-hoc function object...
You may notice I omitted the references from the parameters to
Translate1 because, unfortunately, the standard binders currently
don't allow reference arguments. For more on this problem, see
http://www.boost.org/doc/libs/1_35_0...raits.htm#refs

Alternately, if you have the standard library extensions known as TR1
or if you have Boost
Mmmh... I was not aware of that.
I may have to review my design, or to use Pete Becker's TR1 or Boost
as you suggest.
The point is that I have to call a code analogous to the circles'
one...
a few millions of times.
And as you told,
You probably want to use references for both of these parameters to
prevent unnecessary copies. (They may be inlined away or relatively
trivial here, but maybe not if you call this a lot.)
I will study this issue.

Cheers,
Giovanni
Aug 13 '08 #5

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

Similar topics

8
3978
by: Alex Vinokur | last post by:
Various forms of argument passing ================================= C/C++ Performance Tests ======================= Using C/C++ Program Perfometer http://sourceforge.net/projects/cpp-perfometer http://alexvn.freeservers.com/s1/perfometer.html
58
10223
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of code... TCHAR myArray; DoStuff(myArray);
5
2163
by: xuatla | last post by:
Hi, I encountered the following compile error of c++ and hope to get your help. test2.cpp: In member function `CTest CTest::operator+=(CTest&)': test2.cpp:79: error: no match for 'operator=' in '*this = CTest::operator+(CTest&)((+t2))' test2.cpp:49: error: candidates are: CTest CTest::operator=(CTest&) make: *** Error 1
17
3619
by: Charles Sullivan | last post by:
The library function 'qsort' is declared thus: void qsort(void *base, size_t nmemb, size_t size, int(*compar)(const void *, const void *)); If in my code I write: int cmp_fcn(...); int (*fcmp)() = &cmp_fcn; qsort(..., fcmp); then everything works. But if instead I code qsort as:
33
2886
by: Martin Jørgensen | last post by:
Hi, In continuation of the thread I made "perhaps a stack problem? Long calculations - strange error?", I think I now got a "stable" error, meaning that the error always seem to come here now (tried: visual studio 2005 + linux/macintosh gcc)... That's a pretty good thing. I think the error still appears using both gcc and visual studio 2005. Everything is standard C (ANSI C ?? I don't know the difference) - but since so many functions...
4
4884
by: Lycan. Mao.. | last post by:
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_;
3
2256
by: alishapal | last post by:
Hi there So I am getting these error messages from Intel (v10) and g++ (v4.1.2) compilers respectively: "error: the object has cv-qualifiers that are not compatible with the member function. object type is: const element" "error: passing 'const Tetelement' as 'this' argument of 'void element:setID(long int)' discards qualifiers" But the same code gets compiled using Microsfot VC8.0 compiler with no errors! After a quick search, I...
6
1357
by: jma | last post by:
Hi all, I want to push some errors on an error stack in my function. The first parameter of the push function is the name of the function where the error occured. For example: int MyFunction() { ... if(error1) pushError("MyFunction","Something is wrong");
8
4441
by: Ruben | last post by:
error: passing `const Weight' as `this' argument of `float Weight::wgt()' discards qualifiers seems to be some sort of standard error format that I'm not understanding. I have code that looks like this header file
0
9990
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
11250
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10849
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...
0
9655
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
8031
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
7183
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();...
0
5872
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6073
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4697
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.