Passing-sth-as-this-argument-of-sth-disgards-qualifiers Error

06 Jun 2012

Today I encountered this compile-time error while using Boost Property Tree library.

Thankfully this is a common error which caused by calling a non-const member function on const object. I fixed this error right after I found the stackoverflow entry stated above. The fix is straightforward: you either modify the non-const member function with a const modifier, or make the object non-const.

To make the explanatiotn more concrete, here is an example of causing such an error:

You declare a member function of a class like this - func_name(arg_list) const, which means not to modify ‘this’ object (the object that member function belongs to or operate on) by modifying any data members of that object. However in that member function, you accidently changed the object’s states (by changing some data members). The fix would be removing the const modifier in the declaration / definition of the function, which means to allow the function to change this object’s states, or, to change the implementation of that function so that it leaves this object’s states intact.

Side note: Here is a clearer explanation of what could have happened.