?
Solved

Compiler Error 2662 - problems using 'const'

Posted on 2004-11-11
4
Medium Priority
?
818 Views
Last Modified: 2012-05-05
Hi,

I have a simple class declared:

class CBall {
      UINT      radius;
      int      dx, dy;
      BallColour      colour;
      CPoint      position;      
public:      
      CBall(const UINT radius, const CPoint position, const BallColour colour);
      CBall();

      // Adjust position/dx/dy accordingly
      void Update();
      void CollideX();
      void CollideY();

      CPoint GetCentre() {return position;}

      const int operator == (const CBall ball);
      const int operator != (const CBall ball);

      const BallColour GetColour() const {return colour;}
      const int GetRadius() const {return radius;}
      
};

Compilation generates the following error:

error C2662: 'GetCentre' : cannot convert 'this' pointer from 'const class CBall' to 'class CBall &'
        Conversion loses qualifiers

If I declare GetCentre as a 'const' function there is no error - but why do I need to do this.. why is the 'this' pointer constant?

Many Thanks,

AT.
0
Comment
Question by:Attack_Trax
  • 2
4 Comments
 
LVL 12

Accepted Solution

by:
stefan73 earned 75 total points
ID: 12555788
Hi Attack_Trax,
CPoint probably is a structure instead of a primitive data type. You should use

const CPoint& GetCentre() {return position;}
or
const CPoint& GetCentre() {return position;}


instead.


Cheers!

Stefan
0
 
LVL 2

Assisted Solution

by:pb_india
pb_india earned 75 total points
ID: 12555802
You are constructing  with const variables:

Try this

CBall(UINT radius, CPoint position, BallColour colour);
0
 

Author Comment

by:Attack_Trax
ID: 12555859
Hi Stefan.

Thanks for your reply - however changing the return type of GetCentre() to 'const CPoint&' does not prevent the error.

Why do I need to declare the body  {return position;} as being constant to prevent this error?

Thanks again,

AT.
0
 

Author Comment

by:Attack_Trax
ID: 12556113
Sorry to waste your time - I have figured it now! The method was being called from a constant CBall instance.. and thus would only allow constant methods to be invoked. Obvious once I worked out where the problem was...

Now it all makes sense :-)

I will split the points for you both - thanks for replying.

AT.
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question