Feb 2, 2011 at 1:28am Feb 2, 2011 at 1:28am UTC
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 Feb 2, 2011 at 1:30am Feb 2, 2011 at 1:30am UTC
Feb 2, 2011 at 1:37am Feb 2, 2011 at 1:37am UTC
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 Feb 2, 2011 at 1:38am Feb 2, 2011 at 1:38am UTC
Feb 2, 2011 at 1:38am Feb 2, 2011 at 1:38am UTC
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 ;
Feb 2, 2011 at 1:39am Feb 2, 2011 at 1:39am UTC
Disch, I owe you my life;)
I had no idea that const functions could only call const functions!
Thank YOU!!