I am trying to implement some getters and setters in an abstract class that I am calling from the derived class draw functions but I can't get the thing to compile and I don't know why. Shape is the abstract base class. I am getting the compiler error that says " passing `const Circle' as `this' argument of `void Shape::SetSize(float, float, float)' discards qualifiers" Below is the code I have for one of the setters and the way I am calling it from the draw functions.

protected:  
      float Sizex, Sizey, Sizez;                          //the member variables for this setter  

 void SetSize(float sx,float sy,float sz);                //the declaration

 void Shape::SetSize(float sx,float sy,float sz){        //the definition  
 this->Sizex = sx;  this->Sizey = sy; this->Sizez = sz;   
 glScalef(Sizex,Sizey,Sizez);                             
 }


 Shape::SetSize(Sizex,Sizey,Sizez);                      // the call from the draw functions

I would greatly apprreciate any help I can get. Much Thanks, Jody Bush

Recommended Answers

All 6 Replies

The problem is not within the code you have posted but rather within the code that call it. I guess you have something like this:

void draw(const Circle& A)
{
    A.SetSize(3,4,5);  // Discards constant!   
}

You have probably set draw to take a constant object but you are passing a reference and thus modifying the object it. The keyword constant protects you from unwanted modifications. Either pass by value or check your constant keywords.

This one compiles fine.

#include <iostream>


class Shape;
class Circle;

class Shape
{
    public:
        Shape():Sizex(0),Sizey(0),Sizez(0){};
        float GetX(void) const {return Sizex;}

    protected:
      float Sizex, Sizey, Sizez;
      void SetSize(float sx,float sy,float sz);
};

void Shape::SetSize(float sx,float sy,float sz)
    {
    this->Sizex = sx;  this->Sizey = sy; this->Sizez = sz;
    }


class Circle:public Shape
    {
    public:
    Circle(){};
    void Draw(int, int, int);
    void Draw(Circle& aCircle);
    };


void Circle::Draw(int x, int y, int z)
    {
    Shape::SetSize(Sizex=y,Sizey=y,Sizez=z);
    }

void Circle::Draw(Circle& aCircle)
    {
    Shape::SetSize(1,2,3);
    }


int main ()
    {
    Circle cir;
    cir.Draw(1,2,3);
    cir.Draw(cir);
    std::cout << "SizeX: " << cir.GetX() << std::endl;
    }

No actually my draw functions don't have any parameters. Below is a copy of one of them.

void Circle::Draw()const{
  Shape::Draw();
 int i;
glPushMatrix();
 Shape::SetSize(Sizex,Sizey,Sizez);
// Shape::SetPosition(x,y,z);
// Shape::SetRotation(Angle,Rotx,Roty,Rotz);
glColor3f(r,g,b);

if(!FILL){   
     
   glEnable(GL_LINE_SMOOTH);
   glLineWidth(2);                          
   glPushMatrix(); 
   glBegin(GL_LINE_LOOP);
   for(i=0; i <= pnts; i++){
   float angle = 2 * PI * R / pnts;
   glNormal3f(cos(angle), 0, -sin(angle));
   angle = 2*PI * i / pnts;
   glVertex3f(x+R * cos(angle),y+R * sin(angle),z);
   }
   glEnd();
   glPopMatrix();  
              
}
else{
   glEnable(GL_POLYGON_SMOOTH);                          
   glPushMatrix(); 
   glBegin(GL_TRIANGLE_FAN);
   for(i=0; i <= pnts; i++){
   float angle = 2 * PI * R / pnts;
   glNormal3f(cos(angle), 0, -sin(angle));
   angle = 2*PI * i / pnts;
   glVertex3f(x+R * cos(angle),y+R * sin(angle),z);
   }
   glEnd();
   glPopMatrix();  
              
  }
glPopMatrix();
}

thanks again, Jody Bush

You do have the "const" keyword in your function definition and declaration. This keywords prevents you from modifying Sizex, Sizey and Sizez. It would also prevent you from altering other member variables.

void Circle::Draw()const{

Keyword "const" protects all your members and the members of your parent class. Delete this keyword in the definition and as well declaration and it should compile. But use "const" to make your code safer, for instance everytime you plan your class member functions not to modify your class members. Thus everything that is within const {..} and modifies your class members causes a compile error. However, "const" helps you to spot easy mistakes before you start to lose controll of your class members as the compiler warns you that you want to change something, that you originally planned not to.

here if you want to make the changes without breaking the code
now , make that x y and z mutable.

dirty but it will help you not to break the code.

here if you want to make the changes without breaking the code
now , make that x y and z mutable.

dirty but it will help you not to break the code.

why would you suggest that? Your just suggesting bad practice.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.