3
votes

A pseudo code (this is my class):

struct cTileState   {
        cTileState(unsigned int tileX, unsigned int tileY, unsigned int texNr) : tileX(tileX), tileY(tileY), texNr(texNr) {}
        unsigned int tileX;
        unsigned int tileY;
        unsigned int texNr;

        bool operator==(const cTileState & r)
        {
            if (tileX == r.tileX && tileY == r.tileY && texNr == r.texNr) return true;
            else return false;
        }
    };

Then I have two containers:

       std::list < std::vector <cTileState> > changesList;  //stores states in specific order
       std::vector <cTileState> nextState;

And somewhere in programm I want to do that in my state swap function:

      if (nextState == changesList.back()) return;

However, when I want to compile it I have some meaningless for me errors, like:

/usr/include/c++/4.7/bits/stl_vector.h:1372:58: required from ‘bool std::operator==(const std::vector<_Tp, _Alloc>&, const std::vector<_Tp, _Alloc>&) [with _Tp = cMapEditor::cActionsHistory::cTileState; _Alloc = std::allocator]’

error: passing ‘const cMapEditor::cActionsHistory::cTileState’ as ‘this’ argument of ‘bool cMapEditor::cActionsHistory::cTileState::operator==(const cMapEditor::cActionsHistory::cTileState&)’ discards qualifiers [-fpermissive]

It says something is wrong in stl_vector.h and that I don't respect const qualifiers, but honestly there are no const qualifiers that I don't respect. What's wrong here?

What's more, ide doesn't show me the error in any specific line in my files - it just displays in build log and that's all.

1
errors are never meaningless.Andreas Grapentin

1 Answers

6
votes

You need to make your member function const, so that it accepts a const this argument:

bool operator==(const cTileState & r) const
                                      ^^^^^

Better yet, make it a free function:

bool operator==(const cTileState &lhs, const cTileState & rhs)

Making the member function const approximately corresponds to the const in const cTileState &lhs, whereas a non-const member function would have a cTileState &lhs equivalent. The function the error is pointing to tries to call it with a const first argument, but your function only accepts a non-const one.