2013-01-12 79 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