Very strange compiler error

I'm sorry, but I have no idea what to even write as topic-title for this.

I have a class World with a function display_pixel, defined:

void display_pixel(const int x, const int y, const RGBColor& color)

and implemented. I need to call this function from another function in the same class, which I do like this:

display_pixel(i, j, pixel_color);

where i and j are both integers, and pixel_color is an instance of a class RGBColor. This line causes the compiler to give me this error:

World.cpp:37: error: passing 'const World' as 'this' argument of 'void World::display_pixel(int, int, const RGBColor&)' discards qualifiers
make[1]: *** [debug/World.o] Error 1



So my question is: what on earth is it even talking about? Why does it think that the function-call is passing an instance of World? This makes absolutely no sense to me at all. I hope someone can help me understand this:p

Thanks,
Fafner
Did you declare display_pixel const? If yes, then that's probably what's causing the error, since the function is trying to change 'this' (which is the current instance) even though it isn't allowed to.
Apart from that you won't get anything from declaring int x and int y const parameters as they are passed by value anyway. RGBColor& color on the other hand should only be const if you are sure that it is not modified in the function.

'this' is the reference to the current object of a class. Whenever you call a member function, a reference to the object is implicitly passed to that function.
For example:
world1.display_pixel(5, 8, rgbvalues);
will implicitly pass a reference to world1 called 'this' to the function display_pixel, so the function knows which object to handle.
Topic archived. No new replies allowed.