passing 'Foo' as 'this' argument ... discards qualifiers

Ok, I've been up all night, my head is swimming and I'm getting complete nonsense from g++. So please excuse me if this is a stupid question :p

The error message from g++ is as follows:

World.cpp:76: error: passing ‘const World’ as ‘this’ argument of ‘double World::max(double, double)’ discards qualifiers


The code in question is this:
1
2
3
4
5
6
7
8
RGBColor World::max_to_one(const RGBColor& c) const {
	float max_value = max(c.r, max(c.g, c.b)); //THIS IS THE LINE
	if (max_value > 1.0) {
		return c / max_value;
	} else {
		return c;
	}
}


and the max()-function:
1
2
3
double World::max(double a, double b) {
	return ((a > b) ? a : b);
}


So my question is: WHAT IS G++ EVEN TALKING ABOUT?

EDIT: float max_value has been changed to double max_value, to no avail
Last edited on
const member functions can only call other const member functions. You can't call a non-const member function from a const one.

Possible solutions (in order in which they're recommended -- #1 is best solution)

1) Get rid of World::max(), instead use std::max (in <algorithm>). std already has this function, no need for your to recreate it.

2) Make World::max() a static function. Since it doesn't use 'this', there's no need for it to be non-static.

3) Make World::max() a const function. Since it doesn't change 'this', it doesn't need to be non-const.
Last edited on
The problem is that World::max has actually 3 arguments:
double max(World *this, double *a, double *b);
And you are using a const world. You need to declare it as:
double World::max(double a, double b) const;
Disch, I owe you my life;)
I had no idea that const functions could only call const functions!
Thank YOU!!
Disch, I owe you my life;)


I plan to collect March 5th, 2013

Make your time.
Topic archived. No new replies allowed.