Hi! I've been developing a CBox class with all kinds of operations working with on Boxes and I'm getting a compiler error I don't understand. The error is "'CBox::volume' : cannot convert 'this' pointer from 'const CBox' to 'CBox &'
1> Conversion loses qualifiers"
I'm getting this 7 times for all of my overloaded operators, what's happening here?
Here's the global overloaded function definitions in "BoxOperators.cpp":
If a member function doesn't modify the class, you must declare it as const: double volume(void) const
Otherwise, the compiler won't let you call it from a const instance of the object.
Yeah I'm not sure why I made the +, * and / operators const but forgot to do it for the accessor and volume functions... stupid me. I threw those in anyhow so thank you :D
Making those operators const is fine, as they don't modify the existing variable (I hope!), just create a new one as the sum/product/quotient of two existing ones.
On the other hand, do not make operator+=, operator*= etc const!
I haven't implemented += and *= operators yet. My point was just I didn't understand why I remembered to make those operators const but not that accessor and volume functions. The +, * and / operators don't modify the existing object of course, I'm not that stupid :D