Help Please!!!

Pages: 12
In this code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
using std::cout;
using std::endl;

class CBox                             // Class definition at global scope
{
  public:
    // Constructor definition
    explicit CBox(double lv = 1.0, double bv = 1.0, double hv = 1.0)
    {
      cout << endl << "Constructor called.";
      m_Length = lv;                   // Set values of
      m_Width = bv;                    // data members
      m_Height = hv;
    }

    // Function to calculate the volume of a box
    double Volume()
    {
      return m_Length*m_Width*m_Height;
    }


  private:
    double m_Length;                   // Length of a box in inches
    double m_Width;                    // Width of a box in inches
    double m_Height;                   // Height of a box in inches

};

int main()
{
  const CBox cigar(8.0, 5.0,1.0);      // Declare cigar box

  cout<<cigar.Volume();

  cout << endl;
  return 0;
}


The output I get is:

1>------ Build started: Project: Tests, Configuration: Debug Win32 ------
1>  Ex7_10A.cpp
1>c:\users\anmol\documents\visual studio 2012\projects\c++ book\code from book\chapter 07\ex7_10a.cpp(35): error C2662: 'CBox::Volume' : cannot convert 'this' pointer from 'const CBox' to 'CBox &'
1>          Conversion loses qualifiers
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


Why is it trying to convert the 'this' pointer to Cbox &? Can someone please explain? I understand the reason of the error completely and I know how to solve it, the 'this' pointer needs to be const and in order to do that you must add a const with the function prototype & definition, but why does it say it is converting?
Last edited on
Object cigar is defined with qualifier const

const CBox cigar(8.0, 5.0,1.0);

So it can call only methods with qulifier const.

Change

1
2
3
4
    double Volume()
    {
      return m_Length*m_Width*m_Height;
    }

to

1
2
3
4
    double Volume() const
    {
      return m_Length*m_Width*m_Height;
    }


I know but why does it say it cant convert? Why is it converting?
The pointer this for const object is in turn const. The compiler can not implicitly convert it to non-const pointer to call non-const function.
Because as vlad said, const objects can only call const functions. It's probably trying to convert the const object to non-const, which it can't do.
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.
Possibly because when a function that is part of an object is called, the object itself gets passed to the function implicitly as a reference (which is why you can access methods/data with this->myData, for instance). the const stops it from performing this conversion.
But isn't 'this' a pointer that points to the object of the function? So why would it try to convert to a reference?

Should I just leave and just say the error was caused by it not being a const and trying to convert so something else?
You are using "referenceing syntax"

cigar.Volume();
Last edited on
Oh so it references Volume? Oh that makes more sense now. It is converting the this pointer to a reference in order to reference volume but it cant cause the object it is pointing to and the pointer itself are both const?

Is that right?
Please reply vlad from moscow! :D
Last edited on
I have not understood what you said but if an object is referenced by a const reference or a const pointer it can access only methods with the qualifier const. For example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>

struct A
{
   void f() { std::cout << "f()\n"; }
   void f() const { std::cout << "f() const\n"; }
};


int main()
{
   A a1;
   a1.f(); // void f() is called

   const A a2;
   a2.f(); // void f() const is called

   const A *pa = &a1; // pa is const pointer a1 is not a const object

   pa->f(); // void f() const is called

   const A &ra = a1;

   ra.f(); // void f() const is called
}
Last edited on
What do you mean if it is referenced by a const reference or a const pointer? What do you mean it is referencing syntax as well?

Last edited on
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 
Oh ok thanks :D

I dont quite understand the part in the Note, (still dont understand the reference part) but atleast I know I understand the problem.

Also could you link me to maybe a documentation or something on this?
> Also could you link me to maybe a documentation or something on this?

The FAQs in this section: http://www.parashift.com/c++-faq/const-correctness.html

> Also a static member (function) does not have the 'this' pointer right?

Right.

Last edited on
I mean about the reference thing not the const. I know what const means but as everyone said something about references and stuff xD.
> I know what const means

That is all that you need to know to understand the error in your original example.

1. const CBox cigar(8.0, 5.0,1.0); defines an object of type const Box

2. Box::Volume() requires that the object on which it is invoked is not a const object; its this pointer must point to a non-const object.

3. cigar.Volume(); is an error; such a this pointer can't be established for the call on cigar because it is a const object.


> I mean about the reference thing not the const. everyone said something about references and stuff xD.

Just ignore all that as white noise.

In particular, the talk of "a const reference" was utter nonsense.

The FAQ linked earlier:
Does "Fred& const x" make any sense?

No, it is nonsense. ... You can't reseat a reference. Never. With or without the const. ...


The IS:
Cv-qualified references are ill-formed except when the cv-qualifiers are introduced through the use of a typedef or of a template type argument, in which case the cv-qualifiers are ignored.


In other words, we can talk of "a reference to const int". But "a const reference to int" makes no sense whatsoever.


Re. pointers: with const CBox cigar(8.0, 5.0,1.0);
and const CBox* ptr = &cigar ;,
the type of ptr is "pointer to const CBox" (not "const pointer to CBox").
@JLBorges
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


The C++ Standard everywhere uses the term const reference. And everybody who reads the Standard understand its meaning. If somebody do not understand its meaning then it is not because const reference has no sense at all. It only means that he does not understand the meaning of the term const reference.:)

@JLBorges
In particular, the talk of "a const reference" was utter nonsense.


Write this to the Standard Committee. Let they fully rewrite the C++ Sandard description.:)
Last edited on
Pages: 12