use*_*947 3 c++ comparison containers list vector
伪代码(这是我的课程):
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;
}
};
Run Code Online (Sandbox Code Playgroud)
然后我有两个容器:
std::list < std::vector <cTileState> > changesList; //stores states in specific order
std::vector <cTileState> nextState;
Run Code Online (Sandbox Code Playgroud)
在程序的某个地方,我想在我的状态交换函数中做到这一点:
if (nextState == changesList.back()) return;
Run Code Online (Sandbox Code Playgroud)
但是,当我想编译它时,我有一些对我来说毫无意义的错误,例如:
/usr/include/c++/4.7/bits/stl_vector.h:1372:58: 来自 'bool std::operator==(const std::vector<_Tp, _Alloc>&, const std::vector<_Tp, _Alloc>&) [with _Tp = cMapEditor::cActionsHistory::cTileState; _Alloc = std::allocator]'
错误:将“const cMapEditor::cActionsHistory::cTileState”作为“bool cMapEditor::cActionsHistory::cTileState::operator==(const cMapEditor::cActionsHistory::cTileState&)”的“this”参数传递会丢弃限定符[-fpermissive]
它说 stl_vector.h 有问题,我不尊重 const 限定符,但老实说,没有我不尊重的 const 限定符。这里有什么问题?
更重要的是,ide 不会在我的文件中的任何特定行中向我显示错误 - 它只显示在构建日志中,仅此而已。
您需要创建您的成员函数const,以便它接受一个const this参数:
bool operator==(const cTileState & r) const
^^^^^
Run Code Online (Sandbox Code Playgroud)
更好的是,让它成为一个免费的功能:
bool operator==(const cTileState &lhs, const cTileState & rhs)
Run Code Online (Sandbox Code Playgroud)
使成员函数const近似对应于constin const cTileState &lhs,而非常量成员函数将具有cTileState &lhs等效项。错误指向的函数试图用const第一个参数调用它,但你的函数只接受一个非常量的。
| 归档时间: |
|
| 查看次数: |
3055 次 |
| 最近记录: |