Compiler Error 2662 - problems using 'const'

Posted on 2004-11-11
Last Modified: 2012-05-05

I have a simple class declared:

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

      // 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,

Question by:Attack_Trax
    LVL 12

    Accepted Solution

    Hi Attack_Trax,
    CPoint probably is a structure instead of a primitive data type. You should use

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



    LVL 2

    Assisted Solution

    You are constructing  with const variables:

    Try this

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

    Author Comment

    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,


    Author Comment

    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.


    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Join & Write a Comment

    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…
    C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (…
    The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
    The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

    754 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

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now