3

So, I'm trying to make a struct TileSet and override the < operator, and then put TileSet's in a priority queue. I've read that I can't call non-const methods on a const reference, but there shouldn't really be a problem, I'm just accessing members, not changing them:

    struct TileSet
    {

        // ... other struct stuff, the only stuff that matters

        TileSet(const TileSet& copy)
        {
            this->gid = copy.gid;
            this->spacing = copy.spacing;
            this->width = copy.width;
            this->height = copy.height;
            this->texture = copy.texture;
        }

        bool operator<(const TileSet &b)
        {
            return this->gid < b.gid;
        }
    }; 

The error message tells me: passing 'const TileSet' as 'this' argument of 'bool TileSet::operator<(const TileSet&)' discards qualifiers [-fpermissive] what does this mean? Changing the variables to const did not work, and I need them to be non-const anyways.

The error occurs when I try to do:

std::priority_queue<be::Object::TileSet> tileset_queue;

  • possible duplicate of [C++ "Passing as this discards qualifiers"](http://stackoverflow.com/questions/10226787/c-passing-as-this-discards-qualifiers) – Jack May 08 '13 at 01:58
  • I guess you could search google before asking: http://stackoverflow.com/questions/10226787/c-passing-as-this-discards-qualifiers http://stackoverflow.com/questions/5973427/error-passing-xxx-as-this-argument-of-xxx-discards-qualifiers http://stackoverflow.com/questions/2412608/g-const-discards-qualifiers – Jack May 08 '13 at 01:59

2 Answers2

6

You need to add a const qualifier to the definition of operator< method:

bool operator<(const TileSet &b) const
                               // ^^^ add me
{
    return this->gid < b.gid;
}

This tells the compiler that the this parameter passed to the function is const, otherwise it will not allow you to pass a const reference as this parameter.

Yang
  • 7,712
  • 9
  • 48
  • 65
0

Try making operator< a const member function.

Richard Johnson
  • 612
  • 1
  • 9
  • 20