473,406 Members | 2,894 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

error: passing `const Weight' as `this' argument of `float Weight::wgt()' discards qualifiers

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
class Weight{
public:
Weight();
explicit Weight(float wgt);
Weight(float wgt, const string &unit);
Weight(string);
float operator=(const float w );
Weight& operator=(const Weight&);
//doesn't work - assignment needs a single argument
//float operator=(const float wgt, const string &unit);

inline float wgt(){ return wgt_; };
float wgt(const float weight);
float wgt(const float weight, const string &unit);
float wgt(const string &descr);
definition or class file

Weight& Weight::operator=(const Weight &fweight)
{
//No choice but to hope it is in kilograms

unit_ = "kg";
wgt_ = fweight.wgt();
return this;
}
it all looks legal to me.

Ruben Safir
--
http://www.mrbrklyn.com - Interesting Stuff
http://www.nylxs.com - Leadership Development in Free Software

So many immigrant groups have swept through our town that Brooklyn, like Atlantis, reaches mythological proportions in the mind of the world - RI Safir 1998

http://fairuse.nylxs.com DRM is THEFT - We are the STAKEHOLDERS - RI Safir 2002

"Yeah - I write Free Software...so SUE ME"

"The tremendous problem we face is that we are becoming sharecroppers to our own cultural heritage -- we need the ability to participate in our own society."

"I'm an engineer. I choose the best tool for the job, politics be damned.<
You must be a stupid engineer then, because politcs and technology have been attached at the hip since the 1st dynasty in Ancient Egypt. I guess you missed that one."

© Copyright for the Digital Millennium

Aug 30 '08 #1
8 4403
Ruben <ru***@www2.mrbrklyn.comkirjutas:
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
class Weight{
public:
[...]
inline float wgt(){ return wgt_; };
Here you define wgt() as a non-const member function.
>
Weight& Weight::operator=(const Weight &fweight)
{
wgt_ = fweight.wgt();
And here you call it through a const Weight reference.

Make your getter methods const member functions.

Besides, why you are using float instead of double? Do you have arrays
with millions of Weight objects? If not, then it makes no sense.

hth
Paavo
Aug 30 '08 #2
On 2008-08-30 08:04, Ruben wrote:
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
class Weight{
public:
Weight();
explicit Weight(float wgt);
Weight(float wgt, const string &unit);
Weight(string);
float operator=(const float w );
Weight& operator=(const Weight&);
//doesn't work - assignment needs a single argument
//float operator=(const float wgt, const string &unit);

inline float wgt(){ return wgt_; };
float wgt(const float weight);
float wgt(const float weight, const string &unit);
float wgt(const string &descr);
definition or class file

Weight& Weight::operator=(const Weight &fweight)
{
//No choice but to hope it is in kilograms

unit_ = "kg";
wgt_ = fweight.wgt();
return this;
}
it all looks legal to me.
Since fweight is const you can not call any non-const methods on it, to
solve the problem declare wgt() as const: "inline float wgt() const {
return wgt_; }".

Or you can change the operator= to:

Weight& Weight::operator=(const Weight &fweight)
{
//No choice but to hope it is in kilograms

unit_ = "kg";
wgt_ = fweight.wgt_;
return this;
}

--
Erik Wikström
Aug 30 '08 #3
On Sat, 30 Aug 2008 01:39:48 -0500, Paavo Helde wrote:
Ruben <ru***@www2.mrbrklyn.comkirjutas:
>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
class Weight{
public:
[...]
> inline float wgt(){ return wgt_; };

Here you define wgt() as a non-const member function.

>Weight& Weight::operator=(const Weight &fweight) {
wgt_ = fweight.wgt();

And here you call it through a const Weight reference.

Make your getter methods const member functions.

Besides, why you are using float instead of double? Do you have arrays
with millions of Weight objects? If not, then it makes no sense.

hth
Paavo

Thanks. My weights are human wweights, so I'm not sure why I need a
double.

What exactly does a const function mean and why is it that when you const
and object that the member functions can't run? Why doesn't const'ing the
object just automatically const the members? and if you try to run a
function that violates that, then it should be a runtime error.
Ruben
--
http://www.mrbrklyn.com - Interesting Stuff
http://www.nylxs.com - Leadership Development in Free Software

So many immigrant groups have swept through our town that Brooklyn, like Atlantis, reaches mythological proportions in the mind of the world - RI Safir 1998

http://fairuse.nylxs.com DRM is THEFT - We are the STAKEHOLDERS - RI Safir 2002

"Yeah - I write Free Software...so SUE ME"

"The tremendous problem we face is that we are becoming sharecroppers to our own cultural heritage -- we need the ability to participate in our own society."

"I'm an engineer. I choose the best tool for the job, politics be damned.<
You must be a stupid engineer then, because politcs and technology have been attached at the hip since the 1st dynasty in Ancient Egypt. I guess you missed that one."

© Copyright for the Digital Millennium

Aug 30 '08 #4
On 2008-08-30 16:25, Ruben wrote:
On Sat, 30 Aug 2008 01:39:48 -0500, Paavo Helde wrote:
>Ruben <ru***@www2.mrbrklyn.comkirjutas:
>>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
class Weight{
public:
[...]
>> inline float wgt(){ return wgt_; };

Here you define wgt() as a non-const member function.

>>Weight& Weight::operator=(const Weight &fweight) {
wgt_ = fweight.wgt();

And here you call it through a const Weight reference.

Make your getter methods const member functions.

Besides, why you are using float instead of double? Do you have arrays
with millions of Weight objects? If not, then it makes no sense.
Please do not quote signatures.
Thanks. My weights are human wweights, so I'm not sure why I need a
double.

What exactly does a const function mean and why is it that when you const
and object that the member functions can't run? Why doesn't const'ing the
object just automatically const the members? and if you try to run a
function that violates that, then it should be a runtime error.
A const member function is a function that makes a promise to not make
any changes to the object. Since non-const member function does not give
this guarantee you can not use them on a const object.

If you want runtime errors whenever you have made an error in you code
you should use a dynamically typed langauge, C++ is statically typed and
the compiler will try to make sure that the program is at least well formed.

--
Erik Wikström
Aug 30 '08 #5
Ruben <ru***@www2.mrbrklyn.comkirjutas:
What exactly does a const function mean and why is it that when you
const and object that the member functions can't run? Why doesn't
It appears you have jumped to C++ programming without any book or tutorial
whatsoever. Make yourself a favor and get a decent book to start with. If
you already know some other language I would suggest Accelerated C++.

(I just wish somebody would have made a similar strong suggestion to me
when I started C++...)

regards
Paavo
Aug 30 '08 #6
On Sat, 30 Aug 2008 01:39:48 -0500, Paavo Helde wrote:
>Weight& Weight::operator=(const Weight &fweight) {
wgt_ = fweight.wgt();

And here you call it through a const Weight reference.

Make your getter methods const member functions.
what does making the fuction const do practically? If I make a
const function that assings some member a value, will it compile?

Ruben

--
http://www.mrbrklyn.com - Interesting Stuff
http://www.nylxs.com - Leadership Development in Free Software

So many immigrant groups have swept through our town that Brooklyn, like Atlantis, reaches mythological proportions in the mind of the world - RI Safir 1998

http://fairuse.nylxs.com DRM is THEFT - We are the STAKEHOLDERS - RI Safir 2002

"Yeah - I write Free Software...so SUE ME"

"The tremendous problem we face is that we are becoming sharecroppers to our own cultural heritage -- we need the ability to participate in our own society."

"I'm an engineer. I choose the best tool for the job, politics be damned.<
You must be a stupid engineer then, because politcs and technology have been attached at the hip since the 1st dynasty in Ancient Egypt. I guess you missed that one."

© Copyright for the Digital Millennium

Aug 30 '08 #7
On Sat, 30 Aug 2008 14:17:54 -0500, Paavo Helde wrote:
>What exactly does a const function mean and why is it that when you
const and object that the member functions can't run? Why doesn't

It appears you have jumped to C++ programming without any book or tutorial
whatsoever. Make yourself a favor and get a decent book to start with. If
you already know some other language I would suggest Accelerated C++.
Thanks. I'm actually using two books, Straughsburg and Lippman, and
neither feel adequate.

I started learning C++ in an NYU class. Sometimes I feel that I didn't
learn anything.

Ruben

--
http://www.mrbrklyn.com - Interesting Stuff
http://www.nylxs.com - Leadership Development in Free Software

So many immigrant groups have swept through our town that Brooklyn, like Atlantis, reaches mythological proportions in the mind of the world - RI Safir 1998

http://fairuse.nylxs.com DRM is THEFT - We are the STAKEHOLDERS - RI Safir 2002

"Yeah - I write Free Software...so SUE ME"

"The tremendous problem we face is that we are becoming sharecroppers to our own cultural heritage -- we need the ability to participate in our own society."

"I'm an engineer. I choose the best tool for the job, politics be damned.<
You must be a stupid engineer then, because politcs and technology have been attached at the hip since the 1st dynasty in Ancient Egypt. I guess you missed that one."

© Copyright for the Digital Millennium

Aug 30 '08 #8
Ruben <ru***@www2.mrbrklyn.comkirjutas:
On Sat, 30 Aug 2008 01:39:48 -0500, Paavo Helde wrote:
>>Weight& Weight::operator=(const Weight &fweight) {
wgt_ = fweight.wgt();

And here you call it through a const Weight reference.

Make your getter methods const member functions.

what does making the fuction const do practically? If I make a
const function that assings some member a value, will it compile?
No, it will not, and that's the point of the const system. It holds your
hand and tries to prevent you shoot in your leg.

Some people argue that constness it not the only or most important aspect
to keep in mind when working with objects, and there should be no
obligation to use it. However, it has been built into the language (in
signatures of assignment op and copy ctor and the rule about binding
temporaries to const references) as well as into the standard library, so
practically there is no alternative to making all code const-correct from
the start.

Here, the standard signature for assignment op is as you wrote:

Weight& Weight::operator=(const Weight &fweight);

The constness of the argument now propagates further to every use of the
fweight reference. I try to explain the propagation mechanism in more
detail, feel free to use your books instead if I am not making any sense
;-)

The signature promises that inside this function the fweight argument is
kept const. Now you are calling function fweight.wgt() which is non-const
and thus may potentially modify the object referenced by fweight. Thus
the compiler cannot be sure it can keep up with the promises of operator=
() signature, so the compilation is aborted.

Now, if you make wgt() const,

class Weight {
public:
float wgt() const { return wgt_; };
Now the operator=() is happy and compiles. OTOH, if you had called some
non-const member function on the same object from wgt(), you would have
got another error there, i.e. the error will also "propagate". Now you
could add 'const' to this next member function and see if the thing
compiles, etc.

However, making the code const-correct this way is quite tedious, better
is to decide up-front which member functions should not modify the
object, and mark them 'const' from the start. The "getter" functions like
wgt() are the obvious candidates.

hth
Paavo

Aug 31 '08 #9

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

Similar topics

20
by: Corno | last post by:
Hi all, There's probably a good reason why a const object can call non const functions of the objects where it's member pointers point to. I just don't see it. For me, that makes the the const...
17
by: DanielESFA | last post by:
Hey guys :) This is a bit of a funny one... We're four guys working on the same project, everybody using KDevelop and g++ on Linux. Three of us are using Mandrake, with g++ 3.4.3 and 3.4.1....
5
by: Mars | last post by:
#include <stdio.h> #include <string.h> int main(void) { char* name="Smith SYN is a man."; char* sure="SyN"; char* s; int x,y;
8
by: Roger Leigh | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 A lot of functions use const pointer arguments. If I have a non-const pointer, it is transparently made const when I pass it to the function, e.g....
9
by: July | last post by:
Hello! consider the following code: class A { public: virtual void f() const{ cout << "A::f()" << endl; } };
2
by: John | last post by:
Hello! When I compile the following code, I get this error message "error: passing ... discards qualifiers" and I don't understand why. Could anybody help me? Thank you! John
20
by: Snis Pilbor | last post by:
Whats the point of making functions which take arguments of a form like "const char *x"? It appears that this has no effect on the function actually working and doing its job, ie, if the function...
4
by: silverburgh.meryl | last post by:
I have code which uses Boost lambda in a template like this: using namespace boost::lambda; template<class T> bool lessThanXY( T& src, T& dest ) { return (src.getY() < dest.getY()); } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...
0
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...
0
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...
0
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...
0
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,...

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.