error C2662: 'CBox::Volume' : cannot convert 'this' pointer from 'const CBox' to 'CBox &'
Conversion loses qualifiers |
> Why is it trying to convert the 'this' pointer to Cbox &?
> I understand the reason of the error completely and I know how to solve it, ...
> but why does it say it is converting?
> But why is it converting it to a cbox reference?
> I understand why it cant convert but why does is it even
> converting and especially to a cbox reference? I'm confused here.
It is a poorly worded diagnostic emitted by the compiler, which is what confused you.
Here's the diagnostic (much clearer) from another compiler:
In function 'int main()' line 35:
error: no matching function for call to 'CBox::Volume() const'
candidate is: double CBox::Volume() <near match>
note: no known conversion for implicit 'this' parameter from 'const CBox*' to 'CBox*' |
Which probably would not have led to any confusion.
You might have also been baffled by the usage "a const reference" or "a const pointer" where what would have been correct was "a const object".
Note: In C++, we can talk about a "reference to const" - for instance "reference to const CBox". But "const reference to CBox" makes absolutely no sense at all. And "a const polinter to CBox" is different from "a pointer to const CBox".
1 2 3 4 5 6 7 8
|
CBox cheroot( 1, 2, 3 ) ; // 'cheroot' is an object of type CBox
const CBox cigar( 4, 5, 6 ) ; // 'cigar' is an object of type const CBox
const CBox& ref = cigar ; // type of 'ref' is reference to const CBox
CBox* const const_ptr = &cheroot ; // type of 'const_ptr' is const pointer to CBox
const CBox* ptr_to_const = &cigar ; // type of 'ptr_to_const' is pointer to const CBox
|