2013-01-12 78 views
2

的伪代码(这是我的课):矢量==操作符

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; 
     } 
    }; 

然后,我有两个容器:

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

而且某处PROGRAMM我想做的事情在我的国家交换功能:

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

然而,当我想编译它,我有一些毫无意义的,我的错误,如:

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

它说什么是错在stl_vector.h和我不尊重const的预选赛,但老实说,有没有,我不尊重const的预选赛。这里有什么问题?

更重要的是,IDE不显示我的任何特定行的错误在我的文件 - 它只是显示在构建日志,这一切。

+0

错误永远不会毫无意义。 –

+0

对,我改变了。 – user1873947

回答

4

你需要让你的成员函数const,使其接受constthis说法:

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

更重要的是,使之成为免费功能:

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

使成员函数const大致对应于在所述const cTileState &lhsconst,而一个非const成员函数将具有cTileState &lhs等效。错误指向的函数试图用const第一个参数来调用它,但是你的函数只接受一个非const函数。

+0

谢谢,它现在可行!问题解决了。 – user1873947