Thread: error: passing const as 'this' argument of ... discards qualifiers..

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    7

    error: passing const as 'this' argument of ... discards qualifiers..

    I've been looking up how to fix this error, apparently it has to do with functions which should be declared const since something they take is const.
    I have a base class, graphobj, which has subclasses triangle, quad, etc.
    Each subclass calls a function, fill, which takes a few parameters, one of which is a Vector3f.

    I'm getting this error:
    graphobj.h: In member function ‘void GraphObj::fill(Image&, Vec3f&, Vec2f)’:
    graphobj.h:13: error: passing ‘const Vec3f’ as ‘this’ argument of ‘int Vec3f:perator!=(const Vec3f&)’ discards qualifiers

    This is the graphobj code:
    Code:
    #ifndef _GRAPHOBJ_H_
    #define _GRAPHOBJ_H_
    
    #include "image.h"
    
    // GraphObj
    
    class GraphObj {
     public:
      virtual ~GraphObj();
      virtual void draw(Image& i) = 0;
      void fill(Image& i, Vec3f& color, Vec2f point) {
        if(i.GetPixel((int)point.x(),(int)point.y()) != color){
          i.SetPixel((int)point.x(),(int)point.y(),color);
          fill(i,color,Vec2f(point.x()+1,point.y()+1));
          fill(i,color,Vec2f(point.x()-1,point.y()+1));
          fill(i,color,Vec2f(point.x()+1,point.y()-1));
          fill(i,color,Vec2f(point.x()-1,point.y()-1));
        }
      }
    };
    
    #endif
    There are quite a few supporting files, I don't want to attach all of them and make this unreadable. If I need to include more information to figure this out, just tell me.

    How do I solve this issue?? Thanks!

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Calling a non-const function from a const function will generate this error.
    I'm not sure what function is const and which isn't, however.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    7
    Here is one of the subclasses:
    Code:
    #include "quad.h"
    #include "image.h"
    #include <string>
    #include <sstream>
    
    Quad::Quad(std::string str){
      stringstream ss(str);
      ss >> fl >> x1 >> y1 >> x2 >> y2 >> x3 >> y3 >> x4 >> y4 >> r >> g >> b;
    }
    
    Quad::~Quad(){}
    
    void Quad::draw(Image& i){
      Vec3f col(r,g,b);
      int avgx = (x1+x2+x3+x4)/4;
      int avgy = (y1+y2+y3+y4)/4;
      i.DrawLine(x1,y1,x2,y2,col);
      i.DrawLine(x2,y2,x3,y3,col);
      i.DrawLine(x3,y3,x4,y4,col);
      i.DrawLine(x4,y4,x1,y1,col);
      if(fl.compare("fill")==0){
        fill(i,col,Vec2f(avgx,avgy));
        fill(i,col,Vec2f((avgx+x1)/2,(avgy+y1)/2));
        fill(i,col,Vec2f((avgx+x2)/2,(avgy+y2)/2));
        fill(i,col,Vec2f((avgx+x3)/2,(avgy+y3)/2));
        fill(i,col,Vec2f((avgx+x4)/2,(avgy+y4)/2));
      }
    }

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Can I assume this line is causing the error?
    Code:
    if(i.GetPixel((int)point.x(),(int)point.y()) != color){
    If so, then how about posting the relevant code to that?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    You should make int Vec3f::operator!=(const Vec3f&) (and probably a few others) const member functions.
    Or even free functions.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    Registered User
    Join Date
    May 2008
    Posts
    7
    This is the GetPixel functino.. it's inside of image.h, which is large, so I wouldn't want to post the entire thing.

    Code:
    const Vec3f& GetPixel(int x, int y) const {
        assert(x >= 0 && x < width);
        assert(y >= 0 && y < height);
        return data[y*width + x]; }

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Try CornedBee's suggestion by making the operator != const, at least.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    May 2008
    Posts
    7
    There has to be another way - the operator overloads were written for me, I do not want to change them.

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    When you start messing with const, you have to make sure the entire path is const or you will get errors somewhere along the way.
    Operator != does not modify the class state, so it should be a const member function and take two const references (for free operator) or one const reference argument (for member function).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help implementing a class
    By jk1998 in forum C++ Programming
    Replies: 8
    Last Post: 04-05-2007, 03:13 PM
  2. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 01:26 AM
  3. vector<>
    By teval in forum C++ Programming
    Replies: 11
    Last Post: 08-18-2003, 03:27 PM
  4. passing discards qualifiers? overloading =...
    By Captain Penguin in forum C++ Programming
    Replies: 9
    Last Post: 10-07-2002, 05:38 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM